Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

Wednesday, June 18, 2014

Clicking problems with IE and Selenium

We were facing couple of problems with selenium tests running on IE, when running the tests locally, we don't have these issues. After adding more logs to the code, i found that sometimes it is failing after sending the click event in different steps. 
Looking online for similar issue, i found that there are clicking issues with IE, as sometimes the element being clicked on will receive the focus, but the click will not be processed by the element.
https://code.google.com/p/selenium/wiki/InternetExplorerDriver#Browser_Focus

To workaround this, i am setting the focus manually to the browser using the below line of code

Driver.SwitchTo().Window(Driver.CurrentWindowHandle);

Monday, June 17, 2013

Selenium: Dealing with HTML loading modal panel

While playing back a test for a java rich faces application, the test was failing in the middle, debugging and playing back step by step, the test was passing with no issues. Looking more into the steps, there was a step that is causing ajax call, and after it there is a button click step, the one that was failing was the next one after the button click. When I checked the screen, there was a loading panel displayed during the ajax call, freezing all the screen, so I figured out that when this loading panel is displayed, selenium was able to find the button and click on it, but it is not actually clicked on the UI of the application, which cause that next step after to fail.

To resolve this, first I tried to look for something to make sure the button is clickable, I found something but for Java, and doesn’t exist for .NET

wait.until(elementToBeClickable(By.partialLinkText("SomeID")));

My test was on IE, and when I tried it with java, I had problems creating the selenium session with the selenium IE server.

Then I thought of something else, to identify any element change in the page that tells that the ajax call is done, then I can use this before the button click, to make sure it is clickable. I looked into the html page before, during and after the ajax call and compared. Before the call and when the loading panel was not visible, I found  hidden div with ID ajaxLoadingModalPanelContainer, it looked like below

<div id="ajaxLoadingModalPanelContainer" class="rich-modalpanel " style="position: absolute; z-index: 100; background-color: inherit; display: none;">

when the loading panel was visible, there was a div in the body like below (same as above, but the display style is gone)

<div id="ajaxLoadingModalPanelContainer" class="rich-modalpanel " style="position: absolute; z-index: 100; background-color: inherit;">

After the ajax call, the loading panel disappears and it changes back to have “display:none”

<div id="ajaxLoadingModalPanelContainer" class="rich-modalpanel " style="position: absolute; z-index: 100; background-color: inherit; display: none;">

I then created a function to wait for the ajaxLoadingModalPanelContainer div to be displayed, then to wait until it is hidden again like below

public void WaitForModalPanel()
{
string element_xpath = ".//*[@id='ajaxLoadingModalPanelContainer' and not(contains(@style,'display: none'))]";
WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 2, 0));
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(element_xpath)));
element_xpath = ".//*[@id='ajaxLoadingModalPanelContainer' and contains(@style,'DISPLAY: none')]";
wait.Until(ExpectedConditions.ElementExists(By.XPath(element_xpath)));
}


Calling the above function before the button click, the button click was also clicked on the UI and the test was passing successfully

Sunday, June 16, 2013

Selenium: unable to locate element using style attribute with internet explorer

While developing a selenium test and running it on IE,  I was trying to locate an element on the page with xpath, selenium couldn’t find the element, I was getting exception: “NoSuchElementException: Unable to find element with xpath == ……”

I thought at the beginning that there is something wrong with my xpath string, and I kept looking into this for a while. I have installed FirePath plugin for Firefox to verify my xpath, and my xpath was working find, xpath could locate element

Finally I spotted something on selenium documentation under section “IE and Style Attributes” that says that if you are trying to locate an element with the style attribute, it might not work in internet explorer as IE interpret the style parameters in upper case differently that other browsers, so locating with style and lowercase will success in Firefox, Chrome, etc… but will fail in IE

2013-06-16_11-23-10

looking back to my xpath, my element xpath was ".//*[@id='ajaxLoadingModalPanelContainer' and contains(@style,'display: none')]"

Changing it to ".//*[@id='ajaxLoadingModalPanelContainer' and contains(@style,DISPLAY: none')]" my test was able to find the element successfully

Monday, April 8, 2013

Running Selenium tests from Visual Studio – Step by step

From Firefox, record new selenium test, for example for Google search

2013-04-07_13-14-39

Export the test to C# web drive

2013-04-07_13-15-06

This is how it looks like after export

2013-04-07_13-15-50

Open visual studio, create new test project

2013-04-07_13-16-36

By default the file UnitTest1.cs is created

2013-04-07_13-25-47

Create a new cs file, name it for example GoogleSearch

2013-04-07_13-25-54

Copy the code from the cs file generated by selenium and paste it in the file GoogleSearch.cs

2013-04-07_13-26-42

Add reference in the project to Selenium .Net WebDriver, WebDriver.dll and WebDriver.Support.dl

2013-04-07_13-29-322013-04-07_13-29-07

Now the references added, you can see that the editor recognizes the selenium name spaces

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

2013-04-07_13-29-40

Find and replace “TestFixture” to “TestClass”, “SetUp” to “TestInitialize()”, “Test” to “TestMethod”, “TearDown” to “TestCleanup()”

2013-04-08_00-31-32

To run with Chrome, download chromedriver.exe from https://code.google.com/p/chromedriver/downloads/list, place it in the bin folder

2013-04-08_09-45-49

Change the driver in the code to Chrome then run the test

driver = new OpenQA.Selenium.Chrome.ChromeDriver();

2013-04-08_09-45-09

Running the test, chrome will open, and Google search page will be displayed, displaying search results then closed

2013-04-08_09-51-36

This was a step by step example on running selenium test from visual studio

Wednesday, April 11, 2012

Running selenium tests from visual studio

I was doing some testing using selenium, the way I was running tests at the beginning is through NUnit,  I thought to give it a try to use the visual studio to run the tests instead.

In visual studio 2010, I created a test project, added a new class file to it, named it selenium test

Added the basic test functions and attributes

the class attribute, and changed the class to be public

    [TestClass()]
public class SeleniumTest


Added Initialization and Cleanup methods



        [TestInitialize()]
public void MyTestInitialize()
{

}

[TestCleanup()]
public void MyTestCleanup()
{

}


Added my test method



        [TestMethod()]
public void TheApplyByQIDWebControlTest()
{

}


Next I took my initial cs file generated from Selenium IDE, opened it, mapped from NUnit functions to VS test functions, so code in [SetUp] went to [TestInitialize()] , code from [TearDown] went to [TestCleanup()] , code from [Test] went to the [TestMethod()]



I also added reference to selenium .NET dlls, built the project with no errors



Next I opened the Test View, my test method appeared in it, I clicked on run, the test started and I was able to run selenium test from visual studio