Driver.SwitchTo().Window(Driver.CurrentWindowHandle);
Wednesday, June 18, 2014
Clicking problems with IE and Selenium
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
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
Export the test to C# web drive
This is how it looks like after export
Open visual studio, create new test project
By default the file UnitTest1.cs is created
Create a new cs file, name it for example GoogleSearch
Copy the code from the cs file generated by selenium and paste it in the file GoogleSearch.cs
Add reference in the project to Selenium .Net WebDriver, WebDriver.dll and WebDriver.Support.dl
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;
Find and replace “TestFixture” to “TestClass”, “SetUp” to “TestInitialize()”, “Test” to “TestMethod”, “TearDown” to “TestCleanup()”
To run with Chrome, download chromedriver.exe from https://code.google.com/p/chromedriver/downloads/list, place it in the bin folder
Change the driver in the code to Chrome then run the test
driver = new OpenQA.Selenium.Chrome.ChromeDriver();
Running the test, chrome will open, and Google search page will be displayed, displaying search results then closed
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