Why my selenium function say no Element found - selenium

Why if i use same code of affordabilityErrorVerify() in mortgageCalculator() function its working fine but when i use that code in affordabilityErrorVerify() [ same as i posted here ] it says : --> org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#ifrm_13536"}
its weird for me can someone help me how i can make it work
public class test1 extends base {
public WebDriver driver;
public static Logger log =LogManager.getLogger(base.class.getName());
#BeforeTest
public void initialize() throws IOException {
driver = initializeDriver(); // initialize the browser driver based on data.properties file browser value
}
#Test(dataProvider = "dataDriven")
public void mortgageCalculator(String amount, String year, String Frequency, String type, String product,
String term, String rate) throws IOException, InterruptedException {
driver.get(prop.getProperty("url")); // read the data.properties file for get the value of url
driver.manage().window().maximize();
LandingPage l = new LandingPage(driver); // created object for Landing page to access page element
Actions actions = new Actions(driver);
WebElement mainMenu = l.menuBar();
actions.moveToElement(mainMenu);
WebElement subMenu = l.clickLink();
actions.moveToElement(subMenu);
actions.click().build().perform();
// Explicit wait because calculator is in frame and it loads after some time
// so wait until frame is visible
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(#class,'col-12 col-md-9 side-content')]")));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,500)");
// switch to frame elements
driver.switchTo().frame(l.switchToFrame());
Thread.sleep(3000);
l.productTabClick().click(); // click on product tab
Thread.sleep(3000);
WebElement money = l.mortgageAmount();
money.click();
money.sendKeys(Keys.CONTROL + "a");
money.sendKeys(Keys.DELETE);
money.sendKeys(amount);
WebElement period = l.mortgageYear();
period.click();
period.sendKeys(Keys.CONTROL + "a");
period.sendKeys(Keys.DELETE);
period.sendKeys(year);
Select s = new Select(l.paymentFrequency());
s.selectByValue(Frequency);
// if data provider send Fixed it will click on fixed radio button otherwise click on variable
if (type == "Fixed") {
l.paymentType().click();
} else {
l.paymentType().click();
}
Select ss = new Select(l.paymentProduct());
ss.selectByValue(product);
Thread.sleep(3000);
driver.switchTo().defaultContent();
js.executeScript("window.scrollBy(0,300)");
driver.switchTo().frame(l.switchToFrame());
Thread.sleep(3000);
WebElement inputOwnRateTerm = l.paymentTerm();
inputOwnRateTerm.click();
inputOwnRateTerm.sendKeys(Keys.CONTROL + "a");
inputOwnRateTerm.sendKeys(Keys.DELETE);
l.paymentTerm().sendKeys(term);
WebElement inputOwnRateValue = l.paymentRate();
inputOwnRateValue.click();
inputOwnRateValue.sendKeys(Keys.CONTROL + "a");
inputOwnRateValue.sendKeys(Keys.DELETE);
l.paymentRate().sendKeys(rate);
inputOwnRateValue.sendKeys(Keys.ENTER);
Thread.sleep(3000);
String actualPayment = l.monthlyPayment().getText();
String actualIOT = l.interestOverTerm().getText();
String actualInterestOverTerm = actualIOT.substring(0, actualIOT.length()-1);
//double actualInterestOverTerm = Math.round(actualIOT)* 10.0) / 10.0;
//System.out.print(actualPayment); // uncomment to see what Mortgage Payment amount function is returning for given data
//System.out.print(actualInterestOverTerm);
//System.out.print(actualIOT);
String totalAmount = amount;
int arg1 = Integer.parseInt(totalAmount);
String mortgageRate = rate;
double arg2 = Double.parseDouble(mortgageRate);
String totalYear = year;
int arg3 = Integer.parseInt(totalYear);
// to find out total Interest over term months based on year
String iot = term;
int iot1 = Integer.parseInt(iot);
int arg4 = iot1 * 12;
// Pass all 4 argument into mortgage calculator to assert actual and expected result
calculator c = new calculator();
double[] expected = c.mortgageCalculator(arg1, arg2, arg3, arg4);
//System.out.println("Mortgage Payment :" + expected[0]); // giving back Mortgage Payment amount from custom function
//System.out.println("Interest over term :" + expected[1]); // giving back Interest over term amount from custom function
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(); // converting numbers into money format [number format]
String act = defaultFormat.format(expected[1]);
String expectedInterestOverTerm = act.substring(0, act.length()-1);
//***********************
// ActualPayment = Getting value from https://www.coastcapitalsavings.com/calculators/mortgage-calculator
// Expected[0] = Getting value from calculator() function which is mortgageCalculator logic file
//***********************
Assert.assertEquals(actualPayment,defaultFormat.format(expected[0])); // Assertion to find out both values are same
Assert.assertEquals(actualInterestOverTerm,expectedInterestOverTerm); // Assertion to find out both values are same
log.info("*************Expected****************");
log.info("Mortgage Payment :" + expected[0]);
log.info("Interest Over Term :" + expectedInterestOverTerm);
log.info("**************Actual*****************");
log.info("Mortgage Payment :" + actualPayment);
log.info("Interest Over Term :" + actualInterestOverTerm);
log.info("_______________________________________");
}
#Test
public void affordabilityErrorVerify() throws InterruptedException
{
LandingPage l = new LandingPage(driver); // created object for Landing page to access page element
JavascriptExecutor js = (JavascriptExecutor) driver;
Thread.sleep(3000);
driver.switchTo().defaultContent();
js.executeScript("window.scrollBy(0,-500)");
Thread.sleep(3000);
driver.switchTo().frame(l.switchToFrame());
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(#class,'col-12 col-md-9 side-content')]")));
Thread.sleep(3000);
l.affordabilityTabClick().click(); // click on affordability tab
Thread.sleep(3000);
driver.findElement(By.cssSelector("slider-control:nth-child(3) > #slider-container #name")).click();
driver.findElement(By.cssSelector("slider-control:nth-child(3) > #slider-container #name")).sendKeys("10000");
driver.findElement(By.cssSelector("slider-control:nth-child(3) > #slider-container #name")).sendKeys(Keys.ENTER);
}

By looking at your code, I have a probable solution to your problem.
There is no priority defined for tests. So in TestNG, if priority is not defined, the test will get executed in alphabetical order. In this case, affordabilityErrorVerify() test will execute first and then mortgageCalculator().
If I observe affordabilityErrorVerify(), there is no method to open URL like driver.get(url) so no page will get open and it will cause NoSuchElementException
A possible answer can be assigned priority to tests
#Test (priority=1, dataProvider = "dataDriven")
public void mortgageCalculator(String amount, String year, String Frequency, String type, String product,
String term, String rate) throws IOException, InterruptedException {
//code
}
#Test (priority=2)
public void affordabilityErrorVerify() throws InterruptedException
{
//code
}
In this as well you have to make sure actions on second test are continuing on same page where test1 ends.
Modify your tests and actions considering flow of test and it will work
Happy coding~

Related

The button in date picker is getting clicked more than the number of clicks needed [duplicate]

I am trying to select a "Depart date" as of 31st october 2018 from the calender https://spicejet.com/ But I am getting error "unknown error: Element is not clickable at point (832, 242). Other element would receive the click: ..." Please help me out. Here is my code getting such exception:
public class bookflight extends Thread {
UtilityMethods utilObj= new UtilityMethods();
#Test
public void SighnUp() throws IOException
{
utilObj.getdriver().get("https://spicejet.com");
utilObj.getdriver().manage().window().maximize();
utilObj.getdriver().findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).click();
utilObj.getdriver().findElement(By.xpath("//a[contains(text(),'Guwahati (GAU)')]")).click();
utilObj.getdriver().findElement(By.xpath("//a[contains(text(),'Goa (GOI)')]")).click();
utilObj.getdriver().findElement(By.className("ui-datepicker-trigger")).click();
utilObj.getdriver().findElement(By.xpath("//div[#class='ui-datepicker-group ui-datepicker-group-first'])/parent:://table[#class='ui-datepicker-calendar']following-sibling::./a/contains(text(),'31')")).click();
}
}
To select From (e.g. Guwahati(GAU)), To (e.g. Goa(GOI)) destination and DEPART DATE as 31/10 within the url https://spicejet.com/ you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
Code Block:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class spicejet_login {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://spicejet.com");
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[#id='ctl00_mainContent_ddl_originStation1_CTXT']"))).click();
driver.findElement(By.xpath("//div[#id='glsctl00_mainContent_ddl_originStation1_CTNR']//table[#id='citydropdown']//li/a[#value='GAU']")).click();
driver.findElement(By.xpath("//div[#id='ctl00_mainContent_ddl_destinationStation1_CTNR']//table[#id='citydropdown']//li/a[#value='GOI']")).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//table[#class='ui-datepicker-calendar']//tr//a[contains(#class,'ui-state-default') and contains(.,'31')]"))).click();
}
}
Browser Snapshot:
There is lots of different factors which results into this exception,
i like to suggest you to try putting some wait.
WebDriverWait wait = new WebDriverWait(utilObj.getdriver(), 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_mainContent_ddl_originStation1_CTXT")));
then try clicking element,
utilObj.getdriver().findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).click();
You can click on element by Action class, based on Exception type:
Actions action = new Actions(driver);
action.moveToElement(WebElement to click).click().perform();
Updated answer to click next date.
//div[contains(#class, 'ui-datepicker-group-first')]//td[#data-month='9' and #data-year='2018']/a[.=31]
You can modify the above XPATH to select date based on YEAR/MONTH/DATE. for more XPath creation go-through my answers.
var path ="//div[contains(#class, 'ui-datepicker-group-first')]//td[#data-month='9' and #data-year='2018']/a[.=31]";
var elem = document.evaluate(path, window.document, null, 9, null ).singleNodeValue;
console.log( elem );
elem.click();
When you enter FROM and TO data, then DEPART DATE field get auto selected. So, just you need to select the first data using javascript.
FROM « //div[#id='ctl00_mainContent_ddl_originStation1_CTNR']//a[#text='Guwahati (GAU)']
TO « //div[#id='ctl00_mainContent_ddl_destinationStation1_CTNR']//a[#text='Goa (GOI)']
DEPART DATE «
//div[contains(#class, 'ui-datepicker-group-first')]//a[contains(#class, 'ui-state-active')]
sample test program.
import io.github.yash777.driver.Browser;
import io.github.yash777.driver.Drivers;
import io.github.yash777.driver.WebDriverException;
public class SpiceJET {
static WebDriver driver;
static WebDriverWait explicitWait;
public static void main(String[] args) throws WebDriverException, IOException {
test();
}
public static void test() throws WebDriverException, IOException {
Drivers drivers = new Drivers();
String driverPath = drivers.getDriverPath(Browser.CHROME, 63, "");
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, driverPath);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
driver = new ChromeDriver( capabilities );
explicitWait = new WebDriverWait(driver, 10);
//Maximize browser window
driver.manage().window().maximize();
//Go to URL which you want to navigate
driver.get("https://spicejet.com/");
clickElement("//input[#id='ctl00_mainContent_ddl_originStation1_CTXT'][1]");
clickElement("//div[#id='ctl00_mainContent_ddl_originStation1_CTNR']//a[#text='Guwahati (GAU)']");
clickElement("//input[#id='ctl00_mainContent_ddl_destinationStation1_CTXT'][1]");
clickElement("//div[#id='ctl00_mainContent_ddl_destinationStation1_CTNR']//a[#text='Goa (GOI)']");
clickUsingJavaScript("//div[contains(#class, 'ui-datepicker-group-first')]//a[contains(#class, 'ui-state-active')]");
}
}
public static void clickElement(String locator) {
By findBy = By.xpath( locator );
WebElement element = explicitWait.until(ExpectedConditions.elementToBeClickable( findBy ));
element.click();
}
public static void clickUsingJavaScript( String locator ) {
StringBuffer click = new StringBuffer();
click.append("var elem = document.evaluate(\""+locator+"\", window.document, null, 9, null ).singleNodeValue;");
click.append("elem.click();");
System.out.println("JavaScript Click.");
jse.executeScript( click.toString() );
}
For Automatic management of Selenium Driver Executable’s in run-time for Java use SeleniumWebDrivers
NOTE: If you are selecting DEPART DATE which got auto selected then selenium throws exception
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error:
Element <input type="text" readonly="readonly" id="ctl00_mainContent_view_date2" class="custom_date_pic required home-date-pick">
is not clickable at point (784, 241). Other element would receive the click: <span class="ui-datepicker-month">...</span>
I hope below code is helpful and handle departure and return date
public class SpicejetDropdowns1 {
public static void main(String[] args) throws InterruptedException
{ System.setProperty("webdriver.chrome.driver","E:\\ChromeDriver\\ChromeDriver2.46\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://www.spicejet.com/");
driver.manage().window().maximize();
System.out.println(driver.getTitle()); driver.findElement(By.cssSelector("#ctl00_mainContent_rbtnl_Trip_1")).click();
// OriginStation
driver.findElement(By.xpath(".//*[#id='ctl00_mainContent_ddl_originStation1_CTXT']")).click();
driver.findElement(By.cssSelector("a[value='DEL']")).click();
// Destination
driver.findElement(By.xpath(".//*[#id='ctl00_mainContent_ddl_destinationStation1_CTXT']")).click();
driver.findElement(By.xpath("(//a[#value='HYD'])[2]")).click();
Thread.sleep(3000); driver.findElement(By.xpath("//input[#id='ctl00_mainContent_view_date1']")).click();
if(driver.findElement(By.id("Div1")).getAttribute("style").contains("1"))
{
System.out.println("its enabled");
Assert.assertTrue(true);
}
else
{
Assert.assertTrue(false);
}
while(!driver.findElement(By.cssSelector("div[class='ui-datepicker-title'] [class='ui-datepicker-month']")).getText().contains("October"))
{
// driver.findElement(By.xpath("//span[contains(text(),'Next')]")).click();
driver.findElement(By.xpath("//a[#class='ui-datepicker-next ui-corner-all']/span[#class='ui-icon ui-icon-circle-triangle-e']")).click();
System.out.println(driver.findElement(By.cssSelector("div[class='ui-datepicker-title'] [class='ui-datepicker-month']")).getText());
}
List<WebElement> dates= driver.findElements(By.xpath("//table[#class='ui-datepicker-calendar']//tr//td"));
int count= dates.size();
for(int i=0; i<count; i++)
{
String txt = driver.findElements(By.xpath("//table[#class='ui-datepicker-calendar']//tr//td")).get(i).getText();
if(txt.equalsIgnoreCase("28"))
{
driver.findElements(By.xpath("//table[#class='ui-datepicker-calendar']//tr//td")).get(i).click();
System.out.println(txt);
break;
}
}
// Return Date Selection
Thread.sleep(3000); driver.findElement(By.xpath("//input[#id='ctl00_mainContent_view_date2']")).click();
while(!driver.findElement(By.cssSelector("div[class='ui-datepicker-title'] [class='ui-datepicker-month']")).getText().contains("October"))
{
// driver.findElement(By.xpath("//span[contains(text(),'Next')]")).click();
driver.findElement(By.xpath("//a[#class='ui-datepicker-next ui-corner-all']/span[#class='ui-icon ui-icon-circle-triangle-e']")).click();
System.out.println(driver.findElement(By.cssSelector("div[class='ui-datepicker-title'] [class='ui-datepicker-month']")).getText());
}
List<WebElement> MDates= driver.findElements(By.xpath("//table[#class='ui-datepicker-calendar']//tr//td"));
int Mcount= dates.size();
for(int i=0; i<Mcount; i++)
{
String txt = driver.findElements(By.xpath("//table[#class='ui-datepicker-calendar']//tr//td")).get(i).getText();
if(txt.equalsIgnoreCase("31"))
{
driver.findElements(By.xpath("//table[#class='ui-datepicker-calendar']//tr//td")).get(i).click();
System.out.println(txt);
break;
}
}
//Select Passengers
Thread.sleep(4000);
driver.findElement(By.xpath(".//*[#id='divpaxinfo']")).click();
Thread.sleep(4000);
WebElement Adults = driver.findElement(By.xpath("//select[#id='ctl00_mainContent_ddl_Adult']")); Select adultsdrp = new Select(Adults);
adultsdrp.selectByValue("2");
WebElement childs = driver.findElement(By.xpath("//select[#id='ctl00_mainContent_ddl_Child']"));
Select childsdrp = new Select(childs);
childsdrp.selectByValue("2");
driver.findElement(By.xpath(".//*[#id='divpaxinfo']")).click();
System.out.println(driver.findElement(By.xpath(".//*[#id='divpaxinfo']")).getText());
//Static Currency Dropdown
WebElement Currency = driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency"));
Select currencydrp = new Select(Currency);
currencydrp.selectByValue("USD"); Assert.assertEquals(driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency")).getAttribute("value"), "USD"); System.out.println(driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency")).getAttribute("value"));
}
}

How can I verifying a footer text using selenium Junit

I want to verify a text in the footer. I normally copied the XPath locator and find element then get the text of the element, but nothing happens for this code part in Junit.
From this website I need to get the footer text:
https://www.qaagility.com/
Here is the code I am using for this. I know there is another way of doing it.
public class QAA_mock3 {
private WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
#Before
public void setUp() {
driver = utils.HelperFunctions.createAppropriateDriver("Chrome");
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
}
#Test
public void test() {
String ExpTitle = "QAAgility" ;
Actions actions = new Actions(driver);
By TwitterLink = By.xpath("//*[#id=\"custom_html-4\"]/div/div[1]/a[2]");
By footer = By.xpath("//div[#id='footer']");
By imglogo = By.cssSelector("img.header-image");
driver.get(" https://www.qaagility.com");
System.out.println("Loaded page...");
String ActTitle = driver.getTitle();
System.out.println("page Title..."+ ActTitle);
if(ActTitle.contains(ExpTitle)) {
System.out.println("Verified the title for ..."+ ExpTitle);
}
else
System.out.println("Title verification went wrong...");
WebElement imgSize = driver.findElement(imglogo);
Dimension imageSize = imgSize.getSize();
int Height = imageSize.getHeight();
int Width = imageSize.getWidth();
System.out.println("The size of the Logo is : Width = "+ Width + "; Height = "+Height);
WebElement Twitter = driver.findElement(TwitterLink);
String Href = Twitter.getAttribute("href");
System.out.println("Twitter Link is :"+ Href);
WebElement footerText = driver.findElement(footer);
System.out.println("WebElement for footer is"+ footerText);
actions.moveToElement(footerText).build().perform();
String FTText = footerText.getText();
System.out.println("Footer Text is :"+ FTText);
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}
You wrong define footer locator:
By footer = By.xpath("//div[#id='footer']");
Try this:
By footer = By.className("copyright-bar")

Selenium not inputting the whole text into a text box

I am using Selenium to clear out the existing Shipping Address fields of a Salesforce Account object and to assign new values. I am coding in C# and running on Visual Studio 2019.
I am getting cases where the textboxes are not getting fully populated.
My code is below.
private string shippingStreet = "56789 Liberty Street"; // 80 character limit
private string shippingCity = "Toronto"; // 40 character limit
private string shippingState = "ON"; // 80 character limit
private string shippingZip = "87654"; // 20 character limit
private string shippingCountry = "Canada"; // 80 character limit
IWebElement shStreet = driver.FindElement(By.XPath("//textarea[#placeholder='Shipping Street']"));
shStreet.Clear();
shStreet.SendKeys(shippingStreet);
IWebElement shCity = driver.FindElement(By.XPath("//input[#placeholder='Shipping City']"));
shCity.Clear();
shCity.SendKeys(shippingCity);
IWebElement shState = driver.FindElement(By.XPath("//input[#placeholder='Shipping State/Province']"));
shState.Clear();
shState.SendKeys(shippingState);
IWebElement shZip = driver.FindElement(By.XPath("//input[#placeholder='Shipping Zip/Postal Code']"));
shZip.Clear();
shZip.SendKeys(shippingZip);
IWebElement shCountry = driver.FindElement(By.XPath("//input[#placeholder='Shipping Country']"));
shCountry.Clear();
shCountry.SendKeys(shippingCountry);
Please see the screenshot.
I fix this issue by adding an extra space after city, state, zip code, and country but I was wondering if there is a better solution.
You can try this method:
Just call it and add the xpath: WaitForElementDisplayed_byXPathTime("//myPath");
WaitForElementDisplayed_byXPathTime
public static void WaitForElementDisplayed_byXPathTime(string value)
{
var wait = new WebDriverWait(Driver, new TimeSpan(0, 0, 30));
wait.Until(webDriver => webDriver.FindElement(By.XPath(value)).Displayed);
}
The other thing I have done on these is create a new type method for individual characters like you would on mobile. This just slows it down a bit.
public static void TypeCharsIndividually(IWebElement element, string expectedValue)
{
//use your code for element displayed and element enabled
element.Click();
element.Clear();
foreach (char c in expectedValue)
{
element.SendKeys(c.ToString());
Thread.Sleep(100);
}
}
java click
public static void ClickJava(IWebElement element)
{
IJavaScriptExecutor executor = driver IJavaScriptExecutor;
executor.ExecuteScript("arguments[0].click();", element);
}
WaitForElement
public static bool WaitForElementDisplayed_byXPath(string path)
{
var result = true;
try { _wait.Until(webDriver => driver.FindElement(By.XPath(path)).Displayed); }
catch (StaleElementReferenceException) { WaitForElementDisplayed_byXPath(path); }
catch (NoSuchElementException) { WaitForElementDisplayed_byXPath(path); }
catch (WebDriverTimeoutException) { result = false; }
return result;
}
This is a Salesforce issue. I see the problem even when i am manually updating a shipping address field and I tab to another field.

Responce time for steps (WebDriver + Jmeter)

I have a test, where I:
Go to the link
Input Login and Password and press Login button
Press Some link
Press LogOut button
I run it in JMeter with 5 users, and I should save some data in csv file, like:
UserName, Login (or smth from 4 steps about), Average time.
In output I should have a file where I can see, that 5 user do step "Login" for average time (5 second for example). How to know average time - find all steps "Login" plus all time and divide on user count (5)?
Implement it in JMeter as follows:
WebDriver Sampler with Label Go to the link
WebDriver Sampler with label Login
WebDriver Sampler with label Navigate
WebDriver Sampler with label Logout
WebDriver code for each sampler should look as follows:
WDS.sampleResult.sampleStart()
// put code for login, navigate, logout, etc.
WDS.sampleResult.sampleEnd()
WebDriver session will remain between samplers and JMeter is smart enough to measure average response times, just add a relevant listener, i.e. Aggregate Report
See The WebDriver Sampler: Your Top 10 Questions Answered guide for more tips and tricks.
No, it's not passed for me, I use JUnit. Here's my code:
public class LoadTestTwo extends TestCase {
private WebDriver driver;
public FirefoxProfile profile = new FirefoxProfile();
public int index=0;
private long start;
private long end;
boolean alreadyExists = new File("C:\\output.csv").exists(); //write estimate time to file
public LoadTestTwo(){
reset();
//start = System.currentTimeMillis();
}
public void end(){
end = System.currentTimeMillis();
}
public long duration(){
return (end-start);
}
public void reset(){
start = 0;
end = 0;
}
public LoadTestTwo(String testName){
super(testName);
}
#Before
public void setUp() throws Exception {
super.setUp();
}
#Test
public void testTestLoad() throws InterruptedException, IOException, FileNotFoundException {
LoadTestTwo t = new LoadTestTwo();
try {
CsvWriter csvOutput = new CsvWriter(new FileWriter("C:\\output.csv",true),',');
if (!alreadyExists) {
csvOutput.write("Users");
csvOutput.write("Steps");
csvOutput.write("Average Time");
csvOutput.endRecord();
}
driver = new FirefoxDriver();
t.reset();
start = System.currentTimeMillis();
driver.get("somelink"); //just hided the real link
t.end();
csvOutput.write("LoadTest2");
csvOutput.write("Go to URL");
csvOutput.write("" + t.duration());
csvOutput.endRecord();
t.reset();
start = System.currentTimeMillis();
start = System.currentTimeMillis();
driver.findElement(By.id("loginForm:authLogin")).sendKeys("User1");
driver.findElement(By.id("loginForm:authPassword")).sendKeys("123456");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.MILLISECONDS);
driver.findElement(By.id("loginForm:btnLogin")).click();
t.end();
csvOutput.write("LoadTest2");
csvOutput.write("Login");
csvOutput.write("" + t.duration());
csvOutput.endRecord();
driver.manage().timeouts().implicitlyWait(4000, TimeUnit.MILLISECONDS);
t.reset();
start = System.currentTimeMillis();
driver.findElement(By.className("log")).click();
t.end();
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
csvOutput.write("LoadTest2");
csvOutput.write("Go to Administration");
csvOutput.write("" + t.duration());
csvOutput.endRecord();
driver.manage().timeouts().implicitlyWait(7000, TimeUnit.MILLISECONDS);
t.reset();
start = System.currentTimeMillis();
driver.findElement(By.xpath("//a[#class='logout']")).click();
t.end();
csvOutput.write("LoadTest2");
csvOutput.write("Logout");
csvOutput.write("" + t.duration());
csvOutput.endRecord();
/*FileReader fr = new FileReader(new File("C:\\output.csv"));
BufferedReader br = new BufferedReader(fr);
String st;
while ((st = br.readLine()) != null){
System.out.println(st);
}*/
csvOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
#After
public void tearDown() throws Exception {
super.tearDown();
driver.quit();
}
}
Here's just one user, I'll make 5 Jar files and start it in JMeter.

Selenium WebDriver generates StaleElementReferenceExeption on getText() on table elements

The current environment:
Selenium Server version 2.37.0
RemoteWebDriver running on Firefox
no Ajax / asynchronously loaded content
My tests are attempting to validate the content of each cell of an HTML table. Before accessing any table element an explicit wait verifies that the <tbody> element exists
ExpectedCondition<WebElement> recruitTableIsPresent = ExpectedConditions.presenceOfElementLocated(By.id("newRecruitFieldAgentWidget:newRecruitDataTable_data"));
new WebDriverWait(driver, 5).until(recruitTableIsPresent);
Once the table is verified to exist, data is pulled out by row and column
private Stats[] parseStats() {
String xpath = "//tbody[#id='regionalFieldAgentWidget:regionalDataTable_data']/tr[%d]/td[%d]";
Stats[] stats = new Stats[3];
for (int i = 0; i < stats.length; i++) {
String inProgresOrders = cellContent(xpath, i, 1);
String maxCapacity = cellContent(xpath, i, 2);
String allocationRatio = cellContent(xpath, i, 3);
Stats[i] = new Stats(inProgressORders, maxCapacity, allocationRatio);
}
return stats;
}
private String cellContent(String xpathTemplate, int row, int cell) {
String xpath = String.format(xpathTemplate, row + 1, cell + 1);
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
WebElement elementByXPath = driver.findElementByXPath(xpath);
return elementByXPath.getText();
}
I don't see any race conditions, since the table content is populated with the page, and not in an asynchronous call. Additionally, I have seen other answers that suggest invoking findElement() via the driver instance will refresh the cache. Lastly, the explicit wait before accessing the element should ensure that the <TD> tag is present.
What could be causing the getText() method return the following exception:
org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
It's worthwhile to note that the failure is intermittent. Some executions fail while other passes through the same code pass. The table cell causing the failure are also not consistent.
There is a solution to this using Html-Agility-Pack.
This will work only if you want to read the data from that page.
This goes likes this
//Convert the pageContent into documentNode.
void _getHtmlNode(IWebDriver driver){
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(driver.PageSource);
return htmlDocument.DocumentNode;
}
private Stats[] parseStats(){
String xpath = "//tbody[#id='regionalFieldAgentWidget:regionalDataTable_data']/tr[%d]/td[%d]";
Stats[] stats = new Stats[3];
for (int i = 0; i < stats.Length; i++) {
String inProgresOrders = cellContent(xpath, i, 1);
String maxCapacity = cellContent(xpath, i, 2);
String allocationRatio = cellContent(xpath, i, 3);
Stats[i] = new Stats(inProgressORders, maxCapacity, allocationRatio);
}
return stats;
}
private String cellContent(String xpathTemplate, int row, int cell) {
String xpath = String.format(xpathTemplate, row + 1, cell + 1);
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
var documentNode = _getHtmlNode(driver);
var elementByXPath = documentNode.SelectSingleNode(xpath);
return elementByXPath.InnerText;
}
now read any data.
Some tips for using htmlNode.
1. Similar to driver.FindElement: document.SelectSingleNode
2. Similar to driver.FindElements: document.SelectNodes
3. Similar to driver.Text: document.InnerText.
For more search regarding HtmlNode.
Turns out there was a race condition as I've already mentioned. Since jQuery is available via PrimeFaces there is a very handy solution mentioned in a few other posts. I implemented the following method to wait for any asynchronous requests to return before parsing page elements
public static void waitForPageLoad(JavascriptExecutor jsContext) {
while (getActiveConnections(jsContext) > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
}
private static long getActiveConnections(JavascriptExecutor jsContext) {
return (Long) jsContext.executeScript("return (window.jQuery || { active : 0 }).active");
}
Each built in driver implementation implements the JavascriptExecutor interface, so the calling code is very straightforward:
WebDriver driver = new FirefoxDriver();
waitForPageLoad((JavascriptExecutor) driver);