The web application I am trying to automate has a diagnostic tool that allows to ping to a website. It provides output in a box that has a table structure (all of it is included i na ).
I am automating it using Selenium WebDriver and Java to program one. It is structured as a JUnit 4 test and uses WebDriver (not Selenium RC, but the newer one)
Here is what it looks like:
<tr>
<td style="font-family:Arial;font-size:11px;"></td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;"> </td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;">PING ds-any-fp3-real.wa1.b.yahoo.com (98.138.253.109) 56(84) bytes of data.</td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;">64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=1 ttl=53 time=81.9 ms</td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;">64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=2 ttl=53 time=148 ms</td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;">64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=4 ttl=53 time=143 ms</td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;"></td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;">--- ds-any-fp3-real.wa1.b.yahoo.com ping statistics ---</td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;">5 packets transmitted, 3 received, 40% packet loss, time 4012ms</td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;">rtt min/avg/max/mdev = 81.917/124.763/148.373/30.349 ms</td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;"></td>
</tr>
</tbody>
And here is what it looks like on the page:
PING ds-any-fp3-real.wa1.b.yahoo.com (98.138.253.109) 56(84) bytes of data.
64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=1 ttl=53 time=81.9 ms
64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=2 ttl=53 time=148 ms
64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=4 ttl=53 time=143 ms
--- ds-any-fp3-real.wa1.b.yahoo.com ping statistics ---
5 packets transmitted, 3 received, 40% packet loss, time 4012ms
rtt min/avg/max/mdev = 81.917/124.763/148.373/30.349 ms
I need to parse this text using Selenium WebDriver and pass the JUnit test if the ping was successful (doesn't matter if the packets have been lost), I need to extract an IP address as well.
Is there any way I can extract the particular part of the page source (maybe, using driver.getPageSource() in some sophisticated way or finding this piece by xpath and then calling getText()?) and then parse it to get IP out? The way I tried it is as folows:
String IP = "";
String textToParse = //Here, we should have a way to get the string that would contain IP.
String tokenSeparators = "()"; // since our IP is enclosed by brackets
String tokens[] = textToParse.split(tokenSeparators);
for(int i = 0; i<tokens.length; i++){
if(tokens[i].matches("^[1-9]?[1-9]?[1-9]?\\.[1-9]?[1-9]?[1-9]?\\.[1-9]?[1-9]?[1-9]?\\.[1-9]?[1-9]?[1-9]?$")){ // IP regexp
IP = tokens[i]
}
}
Let me know how to extract the text that I need to parse and whether there are errors in my code (for example, if my regexp is right)
Will appreciate any help!
List<WebElement> allTds=driver.findElements(By.cssSelector("td[style*='font-family:Arial;font-size:11px;']");
String allTdText[]=new String[allTds.size()];
int i=0;
for(WebElement eachTd:allTds)
{
allTdText[i++]=eachTd.getText();
}
By using above logic you will get all td tags data in String array. after that parse each individual arraye element as your requirement.
Example :
String a="64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=1 ttl=53 time=81.9 ms";
For getting IP address alone
System.out.println(a.substring(a.indexOf("(")+1,a.indexOf(")")));
it will return 98.138.253.109
Related
Hello and good afternoon, i am in development of my project long story short i need to save my html base page with UTF8 or Unicode
Dim y As String
Dim UTF8encoding() As Byte, MyEncoder As New System.Text.UTF8Encoding(TRUE)
y = (html code should be here will be under)
Dim utf8 As New UTF8Encoding()
Dim utf8EmitBOM As New UTF8Encoding(True)
Dim code As String = y
Path = "C:\Users\OWNER\Desktop\invoice.html"
Try
Dim my_write As System.IO.StreamWriter
my_write = IO.File.CreateText(path)
my_write.write(utf8EmitBOM.GetPreamble())
UTF8encoding = System.Text.Encoding.Convert(System.Text.Encoding.UTF8, System.Text.Encoding.Unicode, MyEncoder.GetBytes(y))
my_write.WriteLine(code)
my_write.Close()
Catch ex As Exception
End Try
HTML
<html>
<style>
table, th, td
{ border: 1px solid black; }
</style>
</head>
<body><center><b>
<font size=20>Family Butcher</font></br></br></br><center><b>
<font size =4>164 Battersea Bridge Road London SW11 3AW</center></font><center><b>
<font size =4>Tel: Mob:</center></font><center><b>
<font size =4>VAT No: 835522334</center></br></font></br>
<table Border = 3 WIDTH=610 align=left></br><tr>
<th colspan=3 align = left>To: " & txto & " <br/>
<br/>Date: <br/><br/>Invoice nº <br/></th></tr>
<td WIDTH = 100 HEIGHT=40><center><b>Quantity </b></td>
<td WIDTH = 400><center><b>Description</b></td><td><center>
<b>Value</b> </br></p>
</body></html>
At the moment i do not know how to save the file in either unicode or utf8 and then i cannot open the html file without the symbols "Â" Thanks for any support
I suggest you use File.WriteAllText - this overload allows you to specify encoding, so you can accomplish your goal with just one line of code, for example:
File.WriteAllText(path, code, Encoding.UTF8)
I have two soy templates in my plugin one of them loads fine but other is not getting loaded with 500 Internal server error. I am attaching the error from console.
Here is the code I have:
{namespace JIRA.Templates.Impacttemplate.ImpactRow}
/**
* Render the information page for the Requirement Template.
* #param actor : string
#param impact: string
*
*/
{template .renderImpactRow}
<tr class="impact-soy" data-key="{$impact}" >
/*<td><span class="key">{$productName}</span></td>*/
<td><span class="key">{$impact}</span></td>
<td><span class="name">{$actor}</span></td>
</tr>
{/template}
Finally I could figure out the solution:
There should not be blank line after soy doc comment.
#param impact: string should be like #param impact : string
Here is the actual working code:
/**
* Render the impact.
* #param new_actor : string
* #param impact : string
*/
{template .renderImpactRow}
<tr class="impact-soy" data-key="{$impact}" >
<td><span class="key">{$impact}</span></td>
<td><span class="name">{$new_actor}</span></td>
</tr>
{/template}
I've been following this tutorial to write a custom selenium formatter: http://seleniumrecipes.com/content/adding-custom-formats.
Comments in a Selenium test script don't follow the general layout
<tr>
<td>pause</td>
<td>6000</td>
<td></td>
</tr>
<!--This is a comment-->
But you still get a command as you loop through the commands in the testCase, but all the values are undefined.
var commands = testCase.commands;
for (var i = 0; i < commands.length; i++) {
var command = commands[i];
result += command.command + ',' + command.target + ',' + command.value + "\n";
}
return result;
I've briefly looked at the source and can't see anything.
Is there a way to get the comments from a Selenium script in a formatter?
Using the HtmlAgilityPack I am trying to obtain the text "9/30/2013" from a node on this website: http://www.nasdaq.com/symbol/goog/financials?query=income-statement&data=quarterly
Here is the HTML from the website
<div id="financials-iframe-wrap">
<br>
<div class="nextgen thin">
<div class="table-headtag">
<div style="float:left;">
<h3 style="color:#fff;">Quarterly Income Statement (values in 000's)</h3>
</div>
<div style="float:right;">
<h3><a id="quotes_content_left_hlswitchtype" href="http://www.nasdaq.com/symbol/goog/financials?query=income-statement" style="color:#fff;">Get Annual Data</a></h3>
</div>
</div>
<div style="clear:both"></div>
<table>
<tbody><tr class="tr_BG_Color">
<th class="th_No_BG">Quarter:</th>
<th style="text-align:left;">Trend</th>
<th>3rd</th>
<th>2nd</th>
<th>1st</th>
<th>4th</th>
</tr>
<tr class="tr_BG_Color">
<th class="th_No_BG">Quarter Ending:</th>
<th></th>
<th>9/30/2013</th>
<th>6/30/2013</th>
<th>3/31/2013</th>
<th>12/31/2012</th>
</tr>
And here is my code
Dim wreq As HttpWebRequest = WebRequest.Create("http://www.nasdaq.com/symbol/goog/financials?query=income-statement&data=quarterly")
wreq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5"
wreq.Method = "get"
Dim prox As IWebProxy = wreq.Proxy
prox.Credentials = CredentialCache.DefaultCredentials
Dim document As New HtmlAgilityPack.HtmlDocument
Dim web As New HtmlAgilityPack.HtmlWeb
web.UseCookies = True
web.PreRequest = New HtmlAgilityPack.HtmlWeb.PreRequestHandler(AddressOf onPreReq)
wreq.CookieContainer = cookies
Dim res As HttpWebResponse = wreq.GetResponse()
document.Load(res.GetResponseStream, True)
Dim Page_Most_Recent_Quarter As Date = document.DocumentNode.SelectSingleNode("//*[#id='financials-iframe-wrap']/div/table//tr[2]/th[3]").InnerText
When my code reaches the last line I get this error Object reference not set to an instance of an object.
If I debug using Debug.WriteLine(document.DocumentNode.SelectSingleNode("//*[#id='financials-iframe-wrap']/div/table/tbody/tr[2]/th[3]")) a blank is returned.
What am I doing wrong?
First of all, why are you creating a HttpWebRequest object? Let the Html Agility Pack do the heavy lifting for you:
Dim doc As New HtmlAgilityPack.HtmlDocument()
Dim web As New HtmlAgilityPack.HtmlWeb()
web.UseCookies = True
doc = web.Load("http://www.nasdaq.com/symbol/goog/financials?query=income-statement&data=quarterly")
Once the HtmlDocument is loaded, we will extract the date:
Dim dateNode As HtmlAgilityPack.HtmlNode = doc.DocumentNode.SelectSingleNode("//*[#id='financials-iframe-wrap']/div/table//tr[2]/th[3]")
If dateNode IsNot Nothing Then
Dim Page_Most_Recent_Quarter As Date = Convert.ToDateTime(dateNode.InnerHtml.Trim())
End If
I tried this several times, and it works perfectly.
<table id="rusTable" class="groupTable" cellspacing="0" cellpadding="0">
<tbody class="ui-sortable" style="">
<tr class="groupTop ruBorder" style="display: table-row;">
<tr id="ru0" class="siru">
<tr class="ruOp off">
<td class="first"></td>
<td colspan="3">
<select class="ruOpSelect">
<option></option>
<option value="AND">AND</option>
<option>AND NOT</option>
<option>OR</option>
</select>
</td>
<td class="last"></td>
</tr>
<tr id="ru1" class="siru">
<tr class="ruOp off">
<td class="first"></td>
<td colspan="3">
<td class="last"></td>
</tr>
<tr id="ru2" class="siru">
<tr class="groupBtm ruBorder" style="display: table-row;">
</tbody>
<tfoot>
</table>
I want to select the AND option
Selenium webdriver code
actions.moveToElement(driver.findElement(By.xpath("//*#id='ruTable']/tbody/tr[3]/td[2]"))).build().perform();
waitForElement(By.xpath("(//*[#id='ruTable']//*[contains(#class,'ruOpSelect')])[1]"),30);
new Select(driver.findElement(By.xpath("(//*[#id='ruTable']//*[contains(#class,'ruOpSelect')])[1]"))).selectByVisibleText("AND");
It does hover action but does not select anything from drop down menu
ERROR - Timed out after 30 seconds waiting for visibility of element located by By.xpath:
(//*[#id='ruTable']//*[contains(#class,'ruOpSelect')])[1]
Try using this xpath, just call click on this:
"//select[#class='ruOpSelect']/option[text()='AND']"
Any drop downs I generally start with the "select" in my xpath and next level should be option.
I use
Thread.sleep(3000);
after the moveToElement action. Works for me. Then I suppose you need to click the element to select it.
You can use this code to select the particular option
IdentifyBy By;
waitForDropDownEnable(By.xpath, "xpath");
WebElement element = findElement(BY.xpath("xpath for the element"));
Select select = new Select(element);
List<WebElement> options = select.getOptions();
String values = "";
for(int index=0; index<options.size(); index++) {
if(!values.equals("")) {
values += ", ";
}
values += options.get(index).getText();
}
select.selectByVisibleText(value);
public void waitForDropDownEnable(final IdentifyBy idBy, final String controlDesc) {
int timeout =30 * 1000;
final long MAX_TIME_OUT = 300000;
final long DELAY = 250;
final long DEAD_LINE = System.currentTimeMillis() + MAX_TIME_OUT;
boolean isEnabled = false;
try {
while(System.currentTimeMillis() <= DEAD_LINE) {
getWebDriver().manage().timeouts().implicitlyWait(0,TimeUnit.SECONDS);
if(findElement(By.xpath("")).isEnabled()) {
isEnabled = true;
break;
}
Thread.sleep(DELAY);
}
} catch (WebDriverException wdex) {
;;
} catch(Exception ex) {
;;
}
}
}
selecting option from hidden drop down menu
in such case i use jsExecutor. Always works for me:
String cssLocator = "tbody.ui-sortable tr.ruOp off td select.ruOpSelect option[value="AND"]";
//find css locator of the needed AND element in dropdown. I use firepath (addon to firebug).
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssLocator+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
Hope this works for you