A website displays the following text I need to assert:
Living Place "123" hasn't been found
I have a piece of ghurkin/cucumber on a webpage I need to assert.assertTrue using Selenum Webdriver Java:
The text "Living Place "123" hasn't been found" is present on the page
The java Code I've written for this, is as follows:
#Then("^The Text \"([^\"]*)\" isnt present on the page$")
public void not_present(String text) throws Throwable {
waitForTextInElementVisible(By.id("main-content"), text);
Assert.assertTrue(driver.findElement(By.id("main-content")).getText().contains(text));
}
The problem is, the Gherkin script can't handle the String this way, as it contains a double quote. Is there a way to assert the exact string as given above?
I am not sure if I get your problem fully.
But you can pass the string as "Living Place \"123\" hasn't been found".
[Note \ before " inside string]
You can call like follows.
not_present("Living Place \"123\" hasn't been found");
try this one #Then("^The Text \"([^\"]*)\"\d+\"([^\"]*)\" isnt present on the page$"). This step mapping will be mapped to string in feature file Then The Text "Living Place "123" hasn't been found" isnt present on the page. This substitutes the text before digits \"([^\"]*)\" i.e. 'Living Place ', this searches more then one digit symbol in quotation marks \"\d+\" i.e 123, and again part to match text.
Apparently, instead of using
\"([^\"]*)\",
I had to use \"(.*)\"
this will make the gherkin script work:
And The text "Living Place "123" hasn't been found" is present on the page
Related
I am working on Cucumber framework, and I have written my feature file and run the test runner. From that I got the snippets, which have to be implemented. I am a bit confused with one as the scenario is that a user types a non-digits string e.g. "nonumbers".
#Given("The string contains {string}")
public void the_string_contains(String string) {
}
As I am unable to just say string = "^[a-zA-Z]+$"; I am not sure how I should define the string as a non-digits string. As it is the #Given, I am not using Pattern in order to check if the string is correctly formated
According to the documentation you can use {string} to match single-quoted or double-quoted strings, for example "banana split" or 'banana split' (but not banana split). Only the text between the quotes will be extracted. The quotes themselves are discarded.
Note that Cucumber expressions (like {string}) are available as of Cucumber-jvm v3.x
For my feature file I did the following implementation. Please check the screenshot:
Feature file
Java file
With the above implementation everything executed just fine.
Okay, so here's the thing: All of you are probably thinking the same thing: you can use
driver.getPageSource();
And this is partially true. The only issue is that the source code gets compiled in a rather strange way where all through the code
\"
starts showing up. I tried removing this manually but that still doesnt fix the problem completely.
One example of what I mean:
normal source code:
\"query_title\":null}",encoded_title:"WyJoZW5rIl0",ref:"unknown",logger_source:"www_main",typeahead_sid:"",tl_log:false,impression_id:"bbdb1882",filter_ids:
Selenium output:
\\\"query_title\\\":null}\",\"encoded_title\":\"WyJoZW5rIl0\",\"ref\":\"br_tf\",\"logger_source\":\"www_main\",\"typeahead_sid\":\"0.6583900225217523\",\"tl_log\":false,\"impression_id\":\"e00060b4\",\"filter_ids\"
It seems to be the same type of thing as where you have to put something in front of certain symbols in quotes, to stop java from seeing it as one of those symbols, but I don't fully understand this behaviour, and have no idea how to fix it... hope you can help :)
edit:
replacing doesn't work because of the way this got compiled. An example of why it won't work is actually in the example I included earlier:
original:
}",encoded_title:
compiled version:
}\",\"encoded_title\":
Replacing \" with " would change it in to:
}","encoded_title":
which differs from the original...
And if I were to replace \" with nothing, I would get:
},encoded_title:
which, sadly, still differs from the original. The way this is compiled I just don't think replacing is a viable option...
You can use javascript to get html using outerHTML or innerHTML (How do I get the HTML source from the page?):
((JavascriptExecutor) driver).executeScript("return document.documentElement.outerHTML;")
((JavascriptExecutor) driver).executeScript("return document.documentElement.outerHTML;")
((JavascriptExecutor) driver).executeScript("return document.all[0].outerHTML")
((JavascriptExecutor) driver).executeScript("return new XMLSerializer().serializeToString(document);")
You can use Java String Class replaceAll method to replace unwanted characters with the character you want.
OLD solution -
driver.getPageSource().replaceAll("\\"", "\"").replaceAll("\\\\", ""));
New approx solution - As page source can contain anything in HTML
public class CheckString {
static String str = "\\\\\\"query_title\\\\\\":null}\\",\\"encoded_title\\":\\"WyJoZW5rIl0\\",\\"ref\\":\\"br_tf\\",\\"logger_source\\":\\"www_main\\",\\"typeahead_sid\\":\\"0.6583900225217523\\",\\"tl_log\\":false,\\"impression_id\\":\\"e00060b4\\",\\"filter_ids\\"";
public static void main(String[] args) {
System.out.println(str.replaceAll("\\\\",","\",")
.replaceAll(":\\\\"", ":\"")
.replaceAll("\\\\"","")
.replaceAll("\\\\\\\\", "\\\\\""));
}
}
OutPut -
\"query_title\":null}",encoded_title:"WyJoZW5rIl0",ref:"br_tf",logger_source:"www_main",typeahead_sid:"0.6583900225217523",tl_log:false,impression_id:"e00060b4",filter_ids
Note - In earlier approach I forgot to escape & character which is used by replaceAll function to separate multiple condition in regex
I have a HTML code
<a id="cmplobremoveTéléphonie+default" class="cmpLOB-remove-btn" onclick="cmpLobRemoveButtonHandler(event, this);" style="text-decoration:none;" href="javascript:void(0)">Retirer</a>
I need to click on the element with xpath as #id='cmplobremoveTéléphonie+default'.
In Cucumber, I have a feature file step as
When Click on remove Button and click on confirm "Téléphonie"
The corresponding method is,
#When("^Click on remove Button and click on confirm \"([^\"]*)\"")
public void remove_Button(String remove) throws Throwable {
String remove_Required = "cmplobremove" +remove +"+default";
driver.baseDriver.findElement(By.id(remove_Required)).click();
Thread.sleep(1000);
driver.baseDriver.findElement(By.linkText("Confirm")).click();
log.info(remove + "is removed");
}
When I try to execute the above step, I'm getting the following NullPointerException,
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"groupIcon+default+T?l?phonie"}
It seems the french text cannot be passed in a similar way to English Text as a string argument. As you can see that Téléphonie is mentioned as T?l?phonie. Seems something to do with UTF-8/UTF-16.
Solutions are appreciated on how to parse the value.
Seems the default file encoder is ANSI for windows. I have changed the encoder to UTF-8 and now it's working without any issues.
try altenative for identifying that element as below using:
driver.baseDriver.findElement(By.xpath("//a[contains(.,'Retirer')]").click();
Thread.sleep(1000);
driver.baseDriver.findElement(By.linkText("Confirm")).click();
I have below text
`h1` text `/h1` `i` text `/i` `u` text `/u`
Here pair h1 /h1 , i /i , u /u perfectly exist so this text should be passed. Now take this text
`h1` text `/h1` `i` text `/i` `u` text `/u
here the u /u combination is missing. So the above text failed.
I tried this
String startTags[] = {"`b`","`h1`","`h2`","`h3`","`h4`","`h5`","`h6`","`ul`","`li`","`i`","`u`"};
String endTags[] = {"`/b`","`/h1`","`/h2`","`/h3`","`/h4`","`/h5`","`/h6`","`/ul`","`/li`","`/i`","`/u`"};
for(int i=0;i<startTags.length;i++){
if(str.indexOf(startTags[i])!=-1){
System.out.println(">>>>"+startTags[i]);
startTagCount++;
}
if(str.indexOf(endTags[i])!=-1){System.out.println("+++"+endTags[i]);
endTagCount++;
}
}
if(startTagCount==endTagCount){
//TEXT IS OK
}else{
// TEXT FAILED
}
It passes below text instead getting failed
`h5`Is your question about programming? `/h5`
`b` bbbbbbbbbbbbbb`/b`
`b` bbbbbbbbbbbbbb`/b
Any better solution or regex in java ?
I'm afraid this problem cannot be solved by (strict) regular expressions, because the language you describe is not a regular language, it extends the language {anbn}, which is a well-known non-regular language.
If all you care about is making sure all opening tags have matching closing tags, then you can use regular expressions.
Your code has a logic problem, in that you count all opening tags and all closing tags, but don't check if the opening tags and closing tags actually match. The startTagCount and endTagCount variables are not sufficient. I would suggest using a map, using the tag type as a key and the value as the count. Increment count on open tag, decrement count on close tag. Check for non-zero after scanning is complete.
What is the grammar of this "language"? Your approach might be not be proper validation. For example, this HTML has matching tag counts but is invalid:
<b><i>Invalid</b></i>
I currently have a problem with Java and Cucumber. Accessing a website's element by using Selenium, I want to use phrases like the following:
Then the value of the attribute XYZ should be 1000
That example is quite trivial and works fine for each attribute name by using the Java annotation
#Then("the value of the attribute (.*) should be (.*)")
except for the following use-case: an attribute name contains parentheses like ABC(s).
While using Eclipse and JUnit, a Cucumber test with a string containing parentheses like that is not even recognized completely but just the part of the string before the opening bracket. Any ideas or solutions?
It doesn't matter whether the attribute name contains any parentheses.
When using this method:
#Then("^the value of the attribute (.*) should be (.*)$")
public void checkAttributeValue(String name, String value)
throws Throwable {
System.out.println("Name: " + name + " value: " + value);
}
And
Then the value of the attribute XYZ(s) should be 1000
I get
Name: XYZ(s) value: 1000
Which I think is what you expect.