Actions and htmlunitdriver - speed issue - selenium

My web application has menus that open on MouseOver. I'm writing tests using htmlunitdriver.
Test code to trigger menu is
Actions builder = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("//a[starts-with(#href,'/index.html')]"));
Thread.sleep(2000);
builder.moveToElement(menu).build().perform();
Thread.sleep(2000);
driver.findElement(By.xpath("//a[starts-with(#href,'/submenuitem')]")).click();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
When I run a single test, it passes just fine. But when I try to run all my 80 tests at once, I get
unable to locate node using //a[starts-with(#href,'/submenuitem'
I guess the submenu is not yet open, htmlunitdriver has too much speed. Somethimes a "You may only interact with elements that are visible is occured on single runs too. Can someone help me fix this issue? Using FirefoxDriver or so is not an option for me.

Using a manual Thread.sleep(time) to wait for selenium actions is a dirty solution and should not be used at all.
Instead you could run a check is the element is visible before interacting with it.
public void waitUntilVisible(WebDriver driver, WebElement element){
WebDriverWait waiting = new WebDriverWait(driver, 10);
waiting.until(ExpectedConditions.visibilityOf(element));
}
public void waitUntilClickable(WebDriver driver, By locator){
WebDriverWait waiting = new WebDriverWait(driver, 10);
waiting.until(ExpectedConditions.elementToBeClickable(locator));
}
Actions builder = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("//a[starts-with(#href,'/index.html')]"));
waitUntilVisible(driver, menu);
builder.moveToElement(menu).build().perform();
WebElement menuItem = driver.findElement(By.xpath("//a[starts-with(#href,'/submenuitem')]"));
waitUntilClickable(driver, By.xpath("//a[starts-with(#href,'/submenuitem')]"));
menuItem.click();

You are using the implicit wait after finding the submenu item. I think there is no use for implicit wait there. The most advisable place to use the implicit wait is to declare after initializing the Driver instance.
One More solution you can use Explicit Wait to wait for an element in the Page.
Refer this post for more info about the Selenium waits.

Related

Click functionality is not working good all the time on CRM application

Sources: Selenium WebDriver, Chrome 73V, ChromeDriver, Java , testNG, CRM application , Eclipse
I am working on web application which is kind of CRM, loaded with tons of UI elements. One test case works today and fail tomorrow. FYI, I used fluent wait for my test cases.
I checked all the xpaths and they are good. On top of this I executed with Debug mode and tests are passing on debug mode.
They are randomly flaky and un-stable , I am not sure what to do to make them stable? I don't want to use thread.sleep , off course.
Below code (just for the idea) I used to click few elements of the page , sometime Action class works sometime doesn't and sometime Click function works sometime doesn't, not sure how to handle such weird scenario?
driver.findElement(By.name("submit")).sendKeys(Keys.ENTER);
OR
driver.findElement(By.name("submit")).sendKeys(Keys.RETURN);
OR
driver.findElement(By.name("submit")).click();
OR
WebElement webElement = driver.findElement(By.id("Your ID Here"));
Actions builder = new Actions(driver);
builder.moveToElement(webElement).click(webElement);
builder.perform();
OR
WebElement webElement = driver.findElement(By.id("Your ID here"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", webElement);
Thanks for your comments, Please see below, this is my fluent wait:
public static boolean waitForElementToBeVisibleOrClickable (WebDriver driver, WebElement element) {
boolean webElement = false;
try {
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
wait = new WebDriverWait(driver, 30)
.ignoring(NoSuchElementException.class,
StaleElementReferenceException.class)
.pollingEvery(200);
wait.until(ExpectedConditions.visibilityOf(element));
**OR**
wait.until(ExpectedConditions.elementToBeClickable(element));
Log.info("Element is visible");
webElement = true;
} catch (Exception e) {
Log.error("Element is not visible");
webElement = false;
} finally {
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
}
return webElement;
}
You defined method to wait, but you don't actually use it. You are locating the element using driver and immediately click it. You should also modify the wait to use it while locating the element, not afterwards
public WebElement waitForElement(By by) {
return wait.until(ExpectedConditions.visibilityOfElementLocated(by));
}
waitForElement(By.name("submit")).click();
You need to take care of the couple of things:
To invoke click() on an element you should always induce WebDriverWait for the elementToBeClickable().
You can find a relevant discussion in How to click a hyperlink without any link text
If the element is within a <form> tag, as an alternative you can use the submit() method.
You can find a detailed discussion in Selenium: submit() works fine, but click() does not
As per the documentation, Do not mix implicit and explicit waits! Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.
You can find a detailed discussion in How to properly configure Implicit / Explicit Waits and pageLoadTimeout through Selenium?
Solution
Instead of using fluent wait you can induce WebDriverWait in conjunction with ExpectedConditions and an optimized code block will be:
public static boolean waitForElementToBeVisibleOrClickable (WebDriver driver, WebElement element) {
try {
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(element));
System.out.println("Element is clickable now");
} catch (TimeoutException e) {
System.out.println("Element isn't clickable");
} finally {
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
}
return element;
}

Selenium webDriver, is it possible for actions to run out of order?

Selenium webDriver, is it possible for actions to run out of order? For example,
WebElement buttonElement = ....
buttonElement.click();
WebElement anotherElement = ....
Actions actions = new Actions(driver);
actions.doubleClick(anotherElement ).perform();
Sometimes not always, The 2nd doubleClick action is executed before the first click(), which causes tests to fail.
Ideally that should not happen.
But you can improve your code by adding web driver wait.
You can initialize webdriverwait in this way :
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement buttonElement = ....
wait.until(ExpectedConditions.elementToBeClickable(buttonElement)).click();
For double click :
WebElement anotherElement = ....
Actions actions = new Actions(driver);
actions.doubleClick(wait.until(ExpectedConditions.elementToBeClickable(anotherElement ))).build().perform();
Try it out, this will make your test cases more stable.

Selenium Web Driver

We are working on IE Automation using Selenium Web driver in C#.Net.
We are getting an exception in handling model popup window. We supposed to do below the action.
When we click on Link button it will open a popup window then we need switch to popup window selecting check box options and click on Submit button.
When clicking on Link button we are able to open the popup window. But here we are facing an issue like the child popup window is not loading with data and getting HTTP 500 Internal server Error.
I don't understand sometimes it was working properly with the same code but not all the times I am getting above issue when I am trying to perform above actions on child window.
is this any IE settings issue or my code issue even i ignored protected mode settings in IE settings.
I am trying with below code :
js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[#id='ByNewNotes']")));
(or)
string jsWindowString = "NewWindow('pop_Type.jsp?Type=External&IuserId=NUVJK50'," + sessionId + ",'400','500');return false";
((IJavaScriptExecutor)driver).ExecuteScript(jsWindowString);
Could you please help on this issue.
Thanks in Advance.
Instead of using ExpectedConditions.ElementEx‌​ists use ExpectedConditions.elementToBeClickable or presenceOfElementLocated
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(""//*[‌​#id='ByNewNotes']")));
element.click();
Either Try to use FluentWait. Create a by function of your element which you want to wait and pass it in below method
WebElement waitsss(WebDriver driver, By elementIdentifier){
Wait<WebDriver> wait =
new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS) .pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
return wait.until(new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
return driver.findElement(elementIdentifier);
}});
}
Hope it will help you :)
Have you tried
Thread.Sleep(2000);
We had the same issues and solved it with this simple way.

Mouse hover on element

http://www.franchising.com/ ---> Mouse over on (Franchises A-Z) ---> need to click Q
I have tried with the following
WebElement we1=driver.findElement(By.cssSelector("a[href='/franchises/']"));
WebElement we2=driver.findElement(By.cssSelector("a[href='/franchises/q.html']"));
String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
((JavascriptExecutor) driver).executeScript(js, we2); // I have used the script since the we2 is not visible
Actions builder=new Actions(driver);
builder.moveToElement(we1).perform();
Thread.sleep(5000);
we2.click();
could any one try and share me the code... Still I'm getting "ElementNotVisibleException"
With firefoxdriver, a lot would depend on what version of driver you are using and what version of Firefox you have on your system since native support would differ based on that.
Following works on Chrome :
WebElement link1 = driver.findElementByLinkText("Franchises A-Z");
Actions action = new Actions(driver);
action.moveToElement(link1).click(driver.findElementByXPath("//a[contains(#href,'franchises/b')]")).perform();
Before going in to the code i just want to you to ensure the version of Selenium server you are using. Please make it to the updated version of 2.28.x
Code:
driver = new FirefoxDriver();
driver.get("http://www.franchising.com/franchises/");
Thread.sleep(5000);
WebElement element=driver.findElement(By.xpath("//tr[3]/td/table/tbody/tr/td[4]/a"));
Actions builder = new Actions(driver);
builder.moveToElement(element).build().perform();
Thread.sleep(5000);
it works fine for me. Try this code. I hope this will work.

Test dynamically loaded content with Selenium Web Driver

I am working on a system that has a web based frontend that I am testing with Selenium. On one page the content is dynamically loaded when scrolling down (maybe you know that from Facebook's friend-list), because it is one of the requirements.
Scrolling down with Selenium Webdriver (I use Chrome) should be no problem via Javascript. But there is a problem with the dynamically added content. How can I make the Webdriver find those elements?
I tried the following to scroll down until no more content is loaded:
int oldSize = 0;
int newSize = 0;
do {
driver.executeScript("window.scrollTo(0,document.body.scrollHeight)");
newSize = driver.findElementsBy(By.cssSelector("selector").size();
} while(newSize > oldSize);
But though the page scrolls down the first time and some now content is loaded correctly, they will not be found by the drivers' findElementsBy(By) function.
Has someone ever faced this problem?? I'd be very glad if someone could help me figuring a solution for that!
Regards, Benjamin
I would recommend using WebDriverWait with ExpectedConditons.
//scroll down with Javascript first
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("selector")));
//interact with your element
element.click()
Take a look at the guidance provided by Selenium Official page:
http://seleniumhq.org/docs/04_webdriver_advanced.html
try using fluent wait in particular. The main feature is:
An implementation of the Wait interface that may have its timeout and polling interval configured on the fly.
Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.
public WebElement fluentWait(final By locator){
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(
new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
}
);
return foo; } ;
The method described returns you web element you can operate with.
So the approach be the following:
1) you need to find the selectors of elements you expect to be rendered after scrolling
e.g.
String cssSelector = "blablabla"
2) scroll down with js
3)
WebElement neededElement = fluentWait(cssSelector);
neededElement.click();
//neededElement.getText().trim();
you can get more info about fluent wait here
I think the problem is waiting for the dynamic content to finish loading. Try to wait 3 seconds just before findElementsBy? In C# the code would be Thread.Sleep(3000);