Showing posts with label Automation. Show all posts
Showing posts with label Automation. Show all posts

Tuesday, 19 June 2012

How to automate onblur through selenium RC .

How to automate onblur through selenium RC .
How to automate lost focus through selenium.


I faced the problem while automating the form submit. All of the form fields are having some action on their lost focus. 
The onblur event occurs when an object loses focus. 
For example : I have two fields First Name and Last Name. When i enter first name and press tab then it loses its focus and onblur function gets called.
and its calls upperCase and it changes the first name in Upper case.
the same case with Last Name. When i enter last name and press tab it calls blur function lowerCase and changes the letters in lower case.

But the problem in automation is i cant automate lost focus. when we type in first name and last name it simply types in first name and last name text box.
It does not call onblur function upper case and lower case and does not change the letter respectively.

so, fireEvent is a special command in selenium which helps in automating onblur function.

this is html of the element and blur javascript functions 







Enter your First Name:


Enter your Last Nast:




when we write simple selenium RC code 

selenium.type("fname", "niraj");
selenium.type("lname", "KUMAR");


it doest call blur functions to change cases of the first name and last name.

But by using fireEvent fuction of selenium we can call blur function. 

selenium.type("fname", "niraj");
selenium.fireEvent("fname", "blur");
selenium.type("lname", "KUMAR");
selenium.fireEvent("lname", "blur");


How this works is :

First this will type "niraj" in First Name and then selenium.fireEvent("fname", "blur"); this will call the onblur fuction "upperCase()" which changes the First Name in upper case "NIRAJ".
then it types in Last Name and then selenium.fireEvent("lname", "blur"); which means it will press tab and lost the function and on the lost focus it calls 
blur function lowerCase which changes the Last Name in lower case.

How to verify the text ignoring cases sensitivity in selenium ?

How to verify the text ignoring cases sensitivity in selenium ?

Some times we need to verify the text on the webpage no matter is upper cases or lower cases or proper.
We just need to verify the text on the web page ignoring their cases sensitivity. 
REGEXPI is used to avoid cases sensitive to verify the text on the page.

Lets say i want to verify the text "Automationtricks is good blog for selenium" No matter its in upper cases or lower case or proper case.

Then use REGEXPI in selenium command selenium.isTextPresent or verifyTextPresent.



verifyTextPresent
regexpi:AUTOMATIONTRICKS IS GOOD BLOG FOR SELENIUM




verifyTextPresent
regexpi:AUTOMATIONTRICKS is good blog for selenium




verifyTextPresent
regexpi:AUTOMAtionTRICKS Is GoOd Blog for SELENIUM




verifyTextPresent
regexpi:automationtricks is good blog for selenium




In Selenium RC 

verifyTrue(selenium.isTextPresent("regexpi:AUTOMATIONTRICKS IS GOOD BLOG FOR SELENIUM"));

verifyTrue(selenium.isTextPresent("regexpi:AUTOMATIONTRICKS is good blog for selenium"));

verifyTrue(selenium.isTextPresent("regexpi:AUTOMAtionTRICKS Is GoOd Blog for SELENIUM"));

verifyTrue(selenium.isTextPresent("regexpi:automationtricks is good blog for selenium"));


Runing test script in multiple browsers using WebDriver

we can run test script in different browsers using web driver. Write one test script and configure in testng xml to run that test case in IE, firefox chrome.





package com.web;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class WebDriverDemo {


private static WebDriver driver = null;

@Test
@Parameters( {"BROWSER"})
public void testread(String BROWSER)throws Exception{

System.out.println("Browser: " + BROWSER);

if (BROWSER.equals("FF")) {
System.out.println("FF is selected");
driver = new FirefoxDriver();
} else if (BROWSER.equals("IE")) {
System.out.println("IE is selected");
driver = new InternetExplorerDriver();
} else if (BROWSER.equals("HU")) {
System.out.println("HU is selected");
driver = new HtmlUnitDriver();
} else if (BROWSER.equals("CH")){
System.out.println("Google chrome is selected");
driver = new ChromeDriver();
}
driver.navigate().to("http://www.yahoo.com");
Thread.sleep(10000);

WebElement search= driver.findElement(By.name("p"));
search.sendKeys("automation blog by niraj");
search.submit();

Thread.sleep(5000);
Assert.assertTrue(driver.getPageSource().contains("automationtricks.blogspot.com"),"Failed in "+ BROWSER);
driver.close();

}
}


In the above sample program BROWSER is a variable which value would be passed from TestNG.xml and TestNG.xml will run the test multiple time each time BROWSER value would be set with different browser name and test will check the BROWSER value and decide which browser test will run.

TestNG.xml



































Easy Way To Automate Using WebDriver

WebDriver is a clean, fast framework for Automation development of web-apps. WebDriver takes many advantages over selenium because selenium is written in java-script which causes a significant weakness: browsers impose a pretty strictly model , try to upload a file ,form control etc.
webDriver takes a different approach to solve the same problem as selenium. Rather than being running a java-script application within a browser, it uses whichever the mechanism is most appropriate to control the browser. Like for Firefox ,this means that webDriver is implemented as an extension.
The real beauty which I like most is real time environment, which means we are more closely modeling how the user interacts with the browser, and that we can type into "file" input elements. WebDriver can make use of facilities offered by the Operating System.
With the benefit of hindsight, we have developed a cleaner, Object-based API for WebDriver, rather than follow Selenium's dictionary-based approach.

Please download the respective jar related to your language from google code selenium project Download 

Now follow below steps:-
1:- Extract the download file into a folder name and include the jars file ion your ClassPath
2:- Sample coding in java





import junit.framework.TestCase;


import org.junit.Test;


import org.openqa.selenium.By;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebDriverTest extends TestCase{


@Test


public void test_hellotest()throws Exception{


WebDriver driver = new FirefoxDriver();


// And now use this to visit Google


// Find the text input element by its name

WebElement element = driver.findElement(By.name("q"));


// Enter something to search for


element.sendKeys("Niraj Kumar");


// Now submit the form. WebDriver will find the form for us from the element


element.submit();


// Check the title of the page


System.out.println("Page title is: " + driver.getTitle());





driver.close();


}

}



How to select value in drop down using Webdriver

How to select values in drop down using Webdriver

This post will take you through the way you can select the value in a drop down. Webdriver does not have straight forward command "SELECT" which Selenium does.
In below example i will open home page of eBay India and search ipod in Consumer Electronic category. This page has a category drop down in which i will select 
Consumer Electronics. 



package com.web;
import java.io.File;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class DropDownSelection {
private static WebDriver driver=null;
@BeforeTest
@org.testng.annotations.Parameters({"BROWSER"})
public void setup(String BROWSER){
if(BROWSER.equals("FF")){
File profileDir = new File("C:\\nir");
FirefoxProfile profile = new FirefoxProfile(profileDir);
profile.setPreference("general.useragent.override", "same user agent string as above");
driver= new FirefoxDriver(profile);
System.out.println("Broser Selected " + BROWSER);
}else if(BROWSER.equals("IE")){
System.out.println("Broser Selected " + BROWSER);
driver = new InternetExplorerDriver();
}else if(BROWSER.equals("HU")){
System.out.println("Broser Selected " + BROWSER);
driver =new HtmlUnitDriver();

} else
{
System.out.println("Broser Selected " + BROWSER);
driver = new ChromeDriver();
}

}
@Test
public void testDropDownSelection() throws InterruptedException{

driver.get("http://www.ebay.in");
WebElement search = driver.findElement(By.name("_nkw"));
search.sendKeys("ipod");

List dd = driver.findElement(By.name("_sacat")).findElements(By.tagName("option"));
for (WebElement option : dd) {
System.out.println(option.getText());
if ("Consumer Electronics".equalsIgnoreCase(option.getText())){
option.click();
break;
}
}
driver.findElement(By.xpath("//input[@value='Search']")).submit();
Thread.sleep(5000);
driver.getPageSource().contains("results found");
driver.quit();
}

}




TestNG.xml 
Using TestNG suite we will run the test case.



xml version="1.0" encoding="UTF-8"?>






<parameter name="BROWSER" value="FF" />










If above code does not work with you configuration just try another way given below.



List options = driver.findElements(By.tagName("option"));
for (WebElement option : options) {
if ("Consumer Electronics".equalsIgnoreCase(option.getText())){
option.click();
}
}



Running test case in multiple browsers parallely in WebDriver

we can run test script in different browsers parallely using web driver. Write one test script and configure in testng xml to run that test case in IE, firefox and chrome parallely.





package com.web;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParallelRunning {

private WebDriver driver=null;
@BeforeTest
@Parameters ({"BROWSER"})
public void setup(String BROWSER){
System.out.println("Browser: " + BROWSER);

if (BROWSER.equals("FF")) {
System.out.println("FF is selected");
driver = new FirefoxDriver();
} else if (BROWSER.equals("IE")) {
System.out.println("IE is selected");
driver = new InternetExplorerDriver();
} else if (BROWSER.equals("HU")) {
System.out.println("HU is selected");
driver = new HtmlUnitDriver();
} else if (BROWSER.equals("CH")){
System.out.println("Google chrome is selected");
driver = new ChromeDriver();
}
}

@Test
public void testParallel()throws Exception{

driver.get("http://www.google.com");
Thread.sleep(5000);
WebElement search = driver.findElement(By.name("q"));
search.sendKeys("automation blog by niraj");
search.submit();
Thread.sleep(5000);
Assert.assertTrue(driver.getPageSource().contains("automationtricks.blogspot.com"));
driver.findElement(By.xpath("//a[contains(@href,'automationtricks.blogspot.com')]")).click();
Thread.sleep(15000);
Assert.assertTrue(driver.getPageSource().contains("working as Associate Manager @CSC"));
driver.quit();

}
}



In the above sample program BROWSER is a variable which value would be passed from TestNG.xml and TestNG.xml will run the test multiple time each time BROWSER value would be set with different browser name and test will check the BROWSER value and decide which browser test will run.

TestNG.xml






































Monday, 28 May 2012

Automate Web Application Testing with Selenium for ABAP


Update: You find the current version of the SeleniumABAP Project at SAP Code Exchange.
Unfortunately I've caught a bad cold and I have to stay in bed exactly to the time I want to travel the Cluetrain to Vienna. Also today, which is the night of the SAP TechEd 2009 Demo Jam, I was not well enough to travel to Vienna. So I had to call Craig Cmehil this morning and canceled my attendance. But as already planned I release the source code of my project to the public. 

Demo Videos

To get an insight of what Selenium and Selenium for ABAP can do for you, please watch this Videos:

Source

The first thing what you need to record tests is Selenium IDE which comes as a Firefox plugin. If you're not running Firefox get it here.
To convert the recorded test in Selenium IDE to a ABAP Unit test class you need abap-rc.js which is a JavaScript file that can be imported into Selenium IDE by navigating to Options - Options ... - Formats in the Selenium IDE Menu. There you can click on the Add button, provide a name for the new format where I would suggest "ABAP Selenium RC" and then paste the code into the box below.
To run the generated ABAP Unit test you have to install the SAPLink Nugget ZSELENIUMABAP.nugg.zip. It provides the Selenium RC ABAP Client Driver class and also the XSL Transformations to create this class from the iedoc.xml which is the Selenium API definition.

I need your support

This is the current status of this project. What I would like to add is a possibility to generate eCATT tests in Selenium IDE and import them as an eCATT test script. If you know a way how to do that please contact me.

Tuesday, 17 April 2012

How to configure selenium scripts through Hudson.


Through this post, I am trying to explain  ‘How to configure Selenium tests in Hudson’. We all know about Selenium and Hudson, these are two well know open source tools. And Hudson is very much helpful for building the selenium scripts in a scheduled manner hence Selenium will take more time to execute tests if suite contains more tests.
Before moving to configuration part we need to create a build.xml file for building the selenium scripts using Ant build. Because I am going to use Ant process to Invoke build.
Preconditions :
Selenium test cases should be able to run through Ant build.
Hudson Setup:
First of all, we need to install Hudson in our system. Its a straight forward and easy step. You can find that in http://wiki.hudson-ci.org/display/HUDSON/Installing+Hudson. After the installation hudson should be up in http://localhost:8080.
Configure the Hudson Jobs for building Selenium scripts:
Now we need to configure selenium Junit test cases in Hudson, for that follow below steps.
1. Click on ‘New Job’ link in the side panel.
2. Enter Job name[say DemoProject]
3. Select ‘Build a free-style software project’ and click on OK button. 
4. You can configure your build in different ways.
  • Build the code from your machine
  • From any of the Source Code Management’ system like CVS, SVN, GIT etc
5. Steps to build test cases from your local machine
5.1 Click on Advanced button under ‘Advanced Project Options’
5.2 Check ‘Use custom workspace’ check box.
5.3 Enter you path where test cases located.
6. Steps to build test cases from any of the ‘Source Code Management’ system like CVS, SVN, GIT [In this step, we don't want use 'custom workspace' as explained above]
6.1 Select any of the system under Source Code Management’ system [say Subversion]
6.2 Enter ‘Repository URL’
6.3 Keep ‘Local module directory’ as .
7. We can configure build time also in Hudson using ‘Build Triggers‘. Currently following build triggers are available
  • Build after other projects are built
  • Poll SCM
  • Build Periodically[generally used one and using syntax of cron]
8. Now, we need to tell Hudson about how do we need to Build the test cases. For that we can use any methods in ‘Add Build Steps’ under ‘Build’ section. We can use either of
  • Execute Windows batch command
  • Invoke Ant
  • Execute Shell
  • Invoke top-level Maven targets.
9. Here I am going to use ‘Invoke Ant’  option and select it from the list.
10. Specify the target name in the ‘Targets’ field.
11. So you finished all the steps for build test cases. Now we need to show the results for that you need to check ‘Publish JUnit test result report’ check box.
12. Enter the path where your reports generates.
13. Click on ‘Save’ button.
Ohh now you are ready for your first Build.
14. Click on ‘Build Now’ button and wait for the build to finish.
You can see the terminal output by clicking ‘Console Output’ link [in corresponding Build page].
Hope it will help you to configure selenium test cases through Hudson.

Continuous Integration with Hudson

by Amit Easow

To give you a little history

Before I discovered Selenium grid and Hudson, I put in place a system for continuous testing using Selenium scripts written in HTML, Cruise Control for continuous integration and Subversion for version control of software code.

Prerequisites:

  • Subversion (http://subversion.tigris.org/getting.html)
  • Selenium Grid (http://selenium-grid.seleniumhq.org/)
  • Hudson (https://hudson.dev.java.net/)
Download and install Selenium Grid. Run the sample demo to get a feel of what you are working with. I decided to teach myself ruby and what better way to do that than to play with an open source system, which recommends it.
Running the test cases locally??
After downloading the selenium grid project, I renamed it to seleniumSystemTests. Then I created my testSuite directory using the code in the examples/ruby directory, as a reference. Copy the Rake file and the equivalent of the spec_helper file from the examples/ruby directory to the testSuite directory. Modify the URL for application under test, in the spec_helper file equivalent.
application_host = ENV['SELENIUM_APPLICATION_HOST'] || "www.comcast.net"

I added my test cases in the testSuite directory and was able to run the test suite locally by following the steps below
In the first terminal (Mac OS X)
  • cd seleniumSystemTests
  • rake hub:start
  • Open http://localhost:4444/console to see the supported browsers
In the second terminal
  • cd seleniumSystemTests
  • rake rc:start_all
  • In the console above you will see 15 remote controls available
In the third terminal
  • cd seleniumSystemTests/testSuite
  • rake tests:run_in_parallel
  • See the tests cases run in parallel on Firefox browsers

Add the seleniumSystemTests project into subversion

svn import seleniumSystemTests https:///path/to/repo/seleniumSystemTests -m "Checking in the selenium project"??

Running the test cases remotely

Now that the test cases are running successfully with the hub and remote controls running on the same local machine, the next logical step is to run the hub and remote controls on separate machines.
Machine A:??
  • Download the seleniumSystemTests project
  • cd seleniumSystemTests
  • rake hub:start
  • Open http://localhost:4444/console to see the browsers supported
Machine B:
  • Check out the seleniumSystemTests project
  • cd seleniumSystemTests
  • rake HOST= HUB_URL=http://:4444 rc:start_all
  • In the console above you will see 15 remote controls available
Go back to Machine A (continuing from where we left off)
  • Open http://localhost:4444/console to see the 15 browsers available
  • Open another terminal to the testSuite directory and run the test cases
rake tests:run_in_parallel
FAIL!!!!!
This is because the remote control server is set to localhost. Open the testSuite directory and find the spec_helper file equivalent. Modify it to say
remote_control_server = ENV['SELENIUM_RC_HOST'] || "" ?? ??

Running the test cases remotely and continuously

Now that the test cases are running remotely, the next logical step is to integrate it with Hudson to run it continuously. Sign into Hudson and configure it as below.
First create a node called Selenium Grid
  • On the Hudson main page, click Manage Hudson link
  • Click on Manage Nodes link
  • Click New Node link
  • Give the node a name, like Selenium Grid
  • Choose Dumb Slave and click Ok button
  • For Name, enter Selenium Grid
  • For Description, enter Selenium Grid??
  • For # of executors, enter 1
  • For Remote FS root, enter your root directory path (/Users/)
  • Leave Labels blank
  • For Usage, choose Leave this machine for tied jobs only
  • For Launch method, choose Launch slave methods via JNLP
  • For Availability, choose Keep this slave on-line as much as possible
  • Click Save button

Secondly create a Hudson project to run the test cases regularly
  • From the Hudson home page, click New Job link.
  • Give the new job a name, like seleniumSystemTests??
  • Choose Build a free-style software project option and click Ok button
  • Tie this project to the node named ?Selenium Grid?
  • Choose subversion option and enter the SVN repo URL
  • Set the build to run 5 min past every hour
  • Enter the commands to execute the tests
cd testSuite
rake tests:run_in_parallel
  • Click ?E-mail notification? option and enter your email address
  • By default, the system is set to send you an email only when the build breaks
  • Click ?Save? button

Finally, click the Build Now button and sit back.??
  • Under Build History find the first build run.??
  • Click the currently running build link
  • Click Console Output link??
  • See the test cases run and the Success message appears at the end

Hope this helps. Now you will receive an email only when the build breaks. In that case, once you open the URL in the email click the Console Output link. Now you can debug the problem.

Running the test cases remotely and continuously

Now that the tests are running continuously against the web application on an hourly basis, the next logic step I am going to take is to run it against more environments. Right now the test cases were running against the Mac/FF environment. Mac/Safari, Windows/IE and Windows/FF are on my list. From my initial investigation it looks like this should work by adding more parameters, indicating the browser and operating system being used, while starting up the remote control and while running the test cases.
Further logical steps include
  • When new code is checked into subversion, Hudson should automatically deploy the application build to a web server
  • Once this completes, a trigger goes off in the Hudson seleniumSystemTests project that imports any new test cases added to subversion. Then it runs the test cases against the new version of the web application.
Look out for further posts when I put the final pieces in place.