Monday, 21 November 2011

Handle Confirm Box in Selenium Webdriver Using Csharp

Here when we click on the below element on confirm box will appear with OK and Cancel buttons. After click on any of these two buttons another alert will open with OK button.

driver.FindElement(By.XPath("//div[@id='content']/table/tbody/tr[6]/td[3]/input")).Click();
IAlert alert = driver.SwitchTo().Alert();    // Switching to alert
string firstalert = alert.Text;              // Trying to get text from the alert
Console.WriteLine(firstalert);              // Display the text on Console
alert.Accept();                             // Click on OK Button
// alert.Dismiss();                         // Click on Cancel Button
IAlert alert1 = driver.SwitchTo().Alert();
string secondalert = alert1.Text;
Console.WriteLine(secondalert);
alert1.Accept();

Friday, 18 November 2011

Working with Mouse Click in selenium webdriver using CSharp

In the below script , i am trying to click on image having name=jack.

Before to do this, we hav to include OpenQA.Selenium.Interactions  namespace  

IWebElement element = driver.FindElement(By.XPath("//img[@name='jack']"));
new Actions(driver).MoveToElement(element).Perform(); //Here i am moving driver to that element.
ILocatable loc = (ILocatable)element;       // Here i am getting co-ordinates of that element.
 IMouse mouse = ((IHasInputDevices)driver).Mouse; // Here i am converting driver as a mouse device.
 mouse.Click(loc.Coordinates);    //Here driver click on that coordinates.

Working with javascript popups in selenium webdriver using CSharp

driver.Navigate().GoToUrl("http://book.theautomatedtester.co.uk");
driver.FindElement(By.LinkText("Chapter1")).Click();
driver.FindElement(By.ClassName("multiplewindow")).Click();
driver.SwitchTo().Window("popupwindow"); // Here popupwindow is name of the window
string text = driver.FindElement(By.CssSelector("p[id='popuptext']")).Text;
Console.WriteLine(text);
driver.FindElement(By.CssSelector("p[id='closepopup']")).Click();
      
         

Tuesday, 1 November 2011

Writing Selenium Tests in Eclipse


First we download Selenium IDE form http://seleniumhq.org/download/. It is a firefox plugin. Click on install
How to install Selenium IDE into your Browser.
Go to http://seleniumhq.org/docs/02_selenium_ide.html#installing-the-ide.
After Installation Selnium IDE will appear in Tools Menu

Click on Record Button on the Right Side corner OR Go to Action---> Click Record

Goto Url and Navigate some steps . After Navigation complete Stop Record.

Then we will get the script in Table format. If you want to source file click on source button. Default HTML code will appear.

If we want to write tests using java or csharp . Go to Options-->Format--->Select format --->Click OK.

If languages are not appear in the Format menu. Go to Options-->Options-->Select Enable Experimental Features. Now, we got the language options.

Copy the Entire code into your Class  Do the following changes in ur script.
1. Change Package name (i.e where our script appears.).  In the previouse example my script contains in com folde. So include this package like package com;
2. Change Untitled Class name to your Class name.
3. Click on Run. 

We will get the below after screenshot

If test passed Green Bar will appear otherwise Red Bar will appear. Don't forget to run selenium server before your test execution.

If you want ot run selenium tests using TestNG , instead of creating JUnit test case create TestNG Class.
Get the script by using TestNG (Remote Control) Format in Selenium IDE. Don't forget to add TestNG Jar file to your project.

Configuring Selenium RC with Eclipse using JUnit

Open Eclipse IDE
1. Select File-->New-->Java Project. Screen will be appear like below screenshot
Enter Project  Name and Click on Finish. On Left Side Bar we will get SeleniumRC Example in Package Explorer

2. Expand Project --> Right Click on Src--->New-->Folder --> enter folder name -->Click Finish
  Like in below screen shot.
3.In Project Explorer we found one package symbol with given folder name. Right Click on that -->New-->Junit Test Case --->Select JUnit3 or JUnit4 Test -->Enter Class name ---->Click Finish.
Like in below screen shot
Expand Package-->One file name with .java extension will appear and scren will be appear like
Right click on Java File-->Got Build Path--> Configur Build Path-->Select Libriaries-->Click on Add External JARs-->Browse for Selenium Source files-->Select Jar files in seleniu-server folder and select jar files present in Selenium Set-up-->Selenium java-client driver

Click OK.

Now Eclipse is Successsfully Configured with Selenium RC. We can write selenium tests in Eclipse

 If JUnit refernce files are not added to your project download JUnit jar file then Add this Jar file like above.

How to install TestNG Plugin in Eclipse

By using following steps we can easily install TestNG plugin in Eclipse

1. Go to Help--->Install Software
2. Click on Add button
3. One pop-up window opened with two text-fields :  Name and Location.
4. Enter TestNG for Name field  and Enter http://beust.com/eclipse/ into location field
5. Click OK .

Select check boxes and click finish. After Successfull installation, we will see the TestNG Test option in
Run Menu-->RunAs-->TestNG Test.







Calling Javascript in Selenium Webdriver

My requirement is to enter different usernames to test registration page. So, i want to generate random usernames by using java script.

Using Csharp

IJavaScriptExecutor js= driver as IJavaScriptExecutor;
string username=(string)js.ExecuteScript("return 'anji'+Math.floor(Math.random()*1111)");



Using Java

JavascriptExecutor js = (JavascriptExecutor) driver;
String username=(String)js.executeScript("return 'anji'+Math.floor(Math.random()*1111)");

We can pass this variable to username filed.