I have a page that I know contains a certain text at a certain xpath. In firefox I use the following code to assert that the text is present:
assertEquals("specific text", driver.findElement(By.xpath("xpath)).getText());
I'm asserting step 2 in a form and confirming that a certain attachment has been added to the form.
However, when I use the same code in Chrome the displayed output is different but does contain the specific text. I get the following error:
org.junit.ComparisonFailure: expected:<[]specific text> but was:<[C:\fakepath\]specific text>
Instead of asserting something is true (exactly what I'm looking for) I'd like to write something like:
assert**Contains**("specific text", driver.findElement(By.xpath("xpath)).getText());
The code above does not work obviously but I can't find how to get this done.
Using Eclipse, Selenium WebDriver and Java
Use:
String actualString = driver.findElement(By.xpath("xpath")).getText();
assertTrue(actualString.contains("specific text"));
You can also use the following approach, using assertEquals:
String s = "PREFIXspecific text";
assertEquals("specific text", s.substring(s.length()-"specific text".length()));
to ignore the unwanted prefix from the string.
Two Methods assertEquals and assertTrue could be used.
Here is the usage
String actualString = driver.findElement(By.xpath("xpath")).getText();
String expectedString = "ExpectedString";
assertTrue(actualString.contains(expectedString));
You can also use this code:
String actualString = driver.findElement(By.xpath("xpath")).getText();
Assert.assertTrue(actualString.contains("specific text"));
Related
If the text is already plain text and passed to the function new HtmlToPlainText().getPlainText() then the new line character is getting added to the result text.
It looks like Jsoup is doing some formatting and adding a line break.
HtmlToPlainText htmlToPlainText = new HtmlToPlainText();
htmlToPlainText.getPlainText(Jsoup.parse(inputString));
I tried outputSettings.prettyPrint(false); but it is not helping.
Input text can be HTML or plain text.
I want the text to be returned as it is(no extra new line) if it is already plain text.
Input: This is the subject for test cnirnv cniornvo cojrpov nmcofrjpv mcprfjv mpcjfpv pvckfpv jvpfkvp cnirv
Output: This is the subject for test cnirnv cniornvo cojrpov nmcofrjpv mcprfjv mpcjfpv \npvckfpv jvpfkvp cnirv.
A new line character is added after mpcjfpv
We can do string replacement but I am looking for a way to do it as part of the library itself.
HtmlToPlainText resides in package org.jsoup.examples, which is not included in the library jar file on Maven Central. In other words, this class is not part of the jsoup API and is only meant for demonstration purposes.
If you want to output the plaintext of a parsed document, try something like this instead:
Document doc = Jsoup.parse("This is the subject for test cnirnv cniornvo cojrpov nmcofrjpv mcprfjv mpcjfpv pvckfpv jvpfkvp cnirv");
System.out.println(doc.text());
I am working on a project where I need to put away some part of string, not to be visible on front page.
I am working with ftl.
Example:
there is a string like:
<#assign valueToShow= "#99#testing,#777#test">
I need to show the values without part #digits#.
The final result need to be like this:
"testing,test"
How can I do that in FTL?
Thanks...
valueToShow?replace("#[0-9]+#", "", "r"), where 3rd "r" parameter means that what you replace is a regular expression.
The string class offers an easy way to do this:
String valueToShow = rawString.replaceFirst("#\\d+#", "")
In Jmeter Bean shell assertion, I'm storing a returned value to a string. The string is returned with an underline. It is displayed with underline. Though I'm not able to show underline in my below text.
https://myhost.com:1234/abc/def/ghi
I have to parse the above string to use it in http reuquerst. For that I'm using URI/URL class to get hostname , port and path etc; But all these works only after I get rid of underline from the text. How do I get rid of underline.
You can remove underscores within the same Beanshell assertion using String.replaceAll() method like:
String myString = "your_string_with_underscores";
myString = myString.replaceAll("_","");
// do what you need with the "sanitized" string
See How to Use BeanShell: JMeter's Favorite Built-in Component article for more Beanshell tips and tricks.
I am having difficulties with Jsoup parser. How can I tell if given string is a valid HTML code?
String input = "Your vote was successfully added."
boolean isValid = Jsoup.isValid(input);
// isValid = true
isValid flag is true, because Jsoup first uses HtmlTreeBuilder: if ony of html, head or body tag is missing, it adds them by itself. Then it uses Cleaner class and checks it against given Whitelist.
Is there any simple way to check if string is a valid HTML without Jsoup attempts to make it HTML?
My example is AJAX response, which comes as "text/html" content type. Then it goes to parser, Jsoup adds this tags and as a result, response is not displayed properly.
Thanks for your help.
First of all, solution proposed by Reuben is not working as expected. Pattern has to be compiled with Pattern.DOTALL flag. Input HTML may have (and probably will) new line signs etc.
So it should be something like this:
Pattern htmlPattern = Pattern.compile(".*\\<[^>]+>.*", Pattern.DOTALL);
boolean isHTML = htmlPattern.matcher(input).matches();
I also think that this pattern should find HTML tag not only . Next: is not the only valid option. There may also be attribute i.e . This also has to be handled.
I chose to modify Jsoup source. If HTMLTreeBuilder (actually state BeforeHtml) tries to add <html> element I throw ParseException and then I am sure that input file was not a valid HTML file.
Use regex to check String contains HTML or not
boolean isHTML = input.matches(".*\\<[^>]+>.*");
If your String contains HTML value then it will return true
String input = "<html><body></body></html>" ;
But this code String input = "Hello World <>"; will return false
I wanted to change width="xyz" , where (xyz) can be any particular value to width="300". I researched on regular expressions and this was the one I am using a syntax with regular expression
String holder = "width=\"340\"";
String replacer="width=\"[0-9]*\"";
theWeb.replaceAll(replacer,holder);
where theWeb is the string
. But this was not getting replaced. Any help would be appreciated.
Your regex is correct. One thing you might be forgetting is that in Java all string methods do not affect the current string - they only return a new string with the appropriate transformation. Try this instead:
String replacement = 'width="340"';
String regex = 'width="[0-9]*"';
String newWeb = theWeb.replaceAll(regex, replacement); // newWeb holds new text
Better use JSoup for manipulating and extracting data, etc. from Html
See this link for more details:
http://jsoup.org/