Selenium Webdriver is very inconsistent during Execution of test scripts - java

We have a Keyword driven framework which was developed using Selenium Webdriver.
While running the scripts some test cases are getting timed out in the first run. When I do the second run the same test cases which failed last time pass but this time some other test cases fail.
Can someone please advise if anything needs to be done on the Framework/Configuration part.
I am using IE9, Java 6, Selenium 2.40 on Windows 7 and IE driver from the official Selenium website.

Your tests could be brittle because of various reasons.
1. Synchronization- DO NOT USE Thread.sleep. You should consider waiting mechanism in your tests.
There are two types of waits in WebDriver. Implicit wait and Explicit
wait.
a. Implicit wait- For example below WebDriver will internally poll at max for 30 seconds before throwing NoSuchElementFoundException
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
b. Explicit wait- Here you are telling WebDriver to wait for a certain condition to satisfy. For example, below I am waiting the link Account to be available to click. Once its available WebDriver will return me the WebElement so that I can click on it. Take a look at some already implemented useful ExpectedConditions
WebDriverWait wait = new WebDriverWait(driver,30/*Timeout in seconds*/);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Account")));
element.click();
2. Data Dependency- Make sure your tests are independent of each other and they don't share data. Tests can collide if they are sharing data which makes them brittle
3. Use CSS over Xpaths - Find my answer here as to why
4. A layer of abstraction- Make sure you have abstracted test logic from page logic. Use PageObjects and PageFactory technics for better maintenance of your suite
Finally read Simon Stewarts blog on Automated Web Testing: Traps for the Unwary for details.

I would suggest making sure that the keywords you are using are not dynamically generated.
I worked on a site once where all of the ID's looked like this: 'ext-gen123', then the next run through, the very same element would have the ID: 'ext-gen124', but the run through after that would have the ID back to 'ext-gen123'...
In this case, you'd have to use some other identifier to locate the element--maybe a CssClass or an XPath.

Sainath,
The Secret to run TestCases efficiently lies in the Way You wrote the TestCases.. Use of Fluent wait, Wait before the Element is Visible and Enabled and use of html ID's in Testcases makes the TCs Efficient. IE and Selenium Does not get along almost all time.. i mean,, There are so many Issues with IE and Selenium.
The Only way to Achieve Efficiency is by Proper Handling of Exceptions and Use of Wait statements

Related

How to wait for WebElement on a web page, which appers sometimes in random places while test execution?

I wrote an automated selenium based tests for a web application and they run perfectly with fast internet connection, but unpredictable behavior with less good connection.
Web application was build so, that if duration of response on a request< of some action at the web page, is bigger than 250ms, then appers loader-wrapper element, that prevents any kind of action from user, until response ends. Loader-wrapper can apper at any request in any place of test execution, so i cant use explicit waits of selenium, because i dont know when and where it will appear. As a result i receive an exception:
org.openqa.selenium.WebDriverException: Element is not clickable at point (411, 675). Other element would receive the click:(.show-component .loader-wrapper)
Is there any way to set a "global wait", which will stop test execution if loader-wrapper appered and will wait until it ends, and then test execution will continue? Or any another idea.
I kind of like your idea of the annotation, but not sure how to implement it.
Another possible approach is to write your own ExpectedCondition "loaderWrapperDisappeared" (or something like that), which would wait for the loader wrapper to be gone, and return the target WebElement so that you could chain a click to it.
You would then use it like this;
(new WebDriverWait(targetWebElement, 50))
.until(ExpectedConditions.loaderWrapperDisappeared(By.id("your div id"))).click();
(pardon the syntax it that's wrong...I haven't written java in a few years)
In case of web driver, you can have to use like this.
WebElement webElement = (new WebDriverWait(driver, 50))
.until(ExpectedConditions.elementToBeClickable(By.id("your div id")));
Here 50 refers to 50 seconds.
For more details, refer below the link.
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/WebDriverWait.html#WebDriverWait-org.openqa.selenium.WebDriver-long-
If I understand correctly you are looking for invisibilityOfElementLocated.
You can add it as a decorator to your steps...
Hope this helps!

Find a unique locator Selenium WebDriver

I am trying to find a unique locator of the "update profile picture button" in my Facebook account to use it for automation test (selenium webdriver with java).
I use driver.findElement(By.className("_156p")).click();, but it doesn't work.
What should I use?!
Being unable to see the rest of the html source, I can't say for sure, but my guess is that the class is actually defined earlier in the source. The problem with using just a class name is that class names do not have to be unique on a page. The unique version of a class name is id, and if you are testing code that you can edit, try to use lots of ids.
If that's not the case, a decent way to deal with code that you can'y edit yourself is to use a css selector. Really good information on doing them here.
Another good debugging option is to use javascript or python to run a webdriver from the terminal. Because these languages are not compiled, you can run them in real time, which can allow you to tweak a class name a lot quicker. If you don't have experience with python, check this out. By using python/javascript you can create the webdriver, and then keep typing in driver.find_element_by_whatever("value_to_find") while on the same page. This will be much quicker than running the java program from scratch for every different "value_to_find".
As the page source you have provided is pretty limited unable to come up with a stable locator. How about getting the div using the text through xpath.
"//div[.='Update Profile Picture']"
Try This:
WebElement element = driver.findElement(By.className("_156n"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Worked for me.

Selenium Webdriver with Java for OBIEE Dashboard Application

I'm new to testing and trying to automate an OBIEE Dashboard application using Selenium Webdriver with Java. But, the problem is, the object identifiers I'm using (class, xpath, etc.) are dynamically generated, which leads to failure of my test case. Is there any way to overcome this? The scope of my test case is limited to testing the UI only.
My advice? Consider if you really, really, need to be using Java/Selenium approach.
A lot of OBIEE testing is better done at the logical layer using nqcmd or ODBC calls into the BI Server. If you really need to test the front end then visual testing is a generally more successful approach. The new Baseline Validation Tool covers both of these.
You can read more detail here:
http://www.rittmanmead.com/2014/01/automated-regression-testing-for-obiee/
http://www.rittmanmead.com/2014/05/visual-regression-testing-of-obiee-with-phantomcss/
http://www.slideshare.net/themoff/smarter-regression-testing-for-obiee-ukoug-15
https://www.youtube.com/watch?v=gMrspsqW0qM
You have to adjust the Generated XPaths as they may be not accurate.
for Example: You want to select this Div
<div class='blueBtn'>Click Me</div>
the XPath generated will be //div[#class='blueBtn']
This may select the first one but if this div is repeatable under another.
You may need to adjust the XPath to select what exactly you want.
So we may be adjust it to be //div[#class='blueBtn' and position()='2']
It's recommended to use the IDs of the elements as they are granted to be unique.
I hope this could help.

How to implement jquery script in selenium webdriver java

I've created a script as the I want to use in another Selenium WebDriver script:
$function() {
$("pane1").hide(300);
});
I'm trying to figure out a way to call this script in my Selenium java code.
Calling jQuery functions from Selenium is done exactly the same way as calling any other. However, there are two issues with your code:
You have $function, where you probably mean $(function. If you tried to execute the code in your question as-is you certainly got an error because of this.
Ok, let's say you fix that problem. Now you have a $(function () {...}) call. This is not harmful but it is pointless as you are essentially saying "execute this function when the page has finished its initial load". If you use Selenium the way it is usually used, it won't return control to you until the page has finished its initial load, so there is no reason to wait for the page load.
So:
((JavascriptExecutor) driver).executeScript("$('pane1').hide(300);");

webdriver sendKeys doesn't wait for page to load

I have an issue with selenium webdriver and i would be very grateful if anyone can help me
Environment:
selenium-server-standalone-2.31.0.jar / selenium-server-standalone-2.35.0.jar
IEDriverServer.exe (tried version 2.28 - 2.35)
Sample code:
WebElement href = this.findElement(By.xpath("//A"));
href.sendKeys(Keys.ENTER);
href.click();
Problem: A fix to any of this would help me
href.sendKeys() successfully simulates user click, but does not wait for page to load
href.click() fails to simulate user click, but successfully wait for page to load
I have search for the source code of .click() method to try to manually create a waitForPageToLoad function, but i haven't been able to find it.
I know i am not giving to much information because the application I am running the test against is internal, so I cant share a link for debugging. But any idea or previous experience with similar problems that could help me figure out what is going on would be appreciate it.
Right now, I have to do both sendKeys and click to achieve the expected results.
Whenever you do .click() on a button or link an internal method something like "waitTillPageLoads()" is called and hence webdriver waits until the next page is loaded completely.
I have experienced a scenario where it waited for almost 10-15 minutes for the page to load because web app was too slow. Wierd it sounds i know.
however sendKeys() doesn't wait. Because sendkeys is not used for clicking purpose rather it used for entering keys.
I guess there must be some strong reason for you to do
href.sendKeys(Keys.ENTER);
And if you still want to go with this approach you can implicitly you can wait for element of next page using implicit wait
WebDriverWait wait = new WebDriverWait(webDriver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath")));
Please note :
You quotation
href.click() fails to simulate user click, but successfully wait for page to load
Can be because you have not set your browser level to 100%.
Please Check my answer at Unable to run Selenium script on IE
What you can do here is after sendKeys operation, put several waits for elements which are on the resulting page using WebDriverWait. This will serve your purpose.

Categories

Resources