Saturday, March 24, 2012

ReportNG + Selenium



ReportNG create better report than TestNG's normal report.

To Start With:

Step 1 : Download reportng-1.1.3.jar and velocity-dep-1.4.jar into classpath

Step 2: Create a testNG.xml file


Sample code for testNG.xml file



Note: In above code TestScripts2 is the class name under test


Step 3: Now right-clicking on that XML file and select "Run as... TestNG suite".


>>The Report generted will be @ test-output/html and are as shown below:




Note: In case you are getting the following error
java.lang.NoClassDefFoundError: com/google/inject/Module
> at java.lang.Class.getDeclaredMethods0(Native Method)


Solution Is -->
1) Disable defaultlisteners , goto Eclipse Project-> Properties -> TestNG -> Diable deafult listeners.
and , 2) Download google-guice-3.0.zip and paste guice-3.0.jar into your classpath.
Now run and check the report in workspace/test-output/html.

Friday, March 23, 2012

Logging in Selenium Webdriver using log4j



Simple four steps to use log4j for logging


Step 1 Add jar file “log4j-1.2.16.jar”
Step 2 Add “log4j.properties” to class path as shown in picture below:
























Code for log4j.properties
# Log levels
# Uncomment the following line to enable full loggin for every class
#log4j.rootLogger=trace, stdout, R
log4j.rootLogger=info, stdout, R

# Console appender configuration
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file.
log4j.appender.R.File=./logs/applog.log
log4j.appender.R.MaxFileSize=500KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
# Rolling File Appender layout
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n


Step 3 import the log4j.logger
















Step 4 usage in code


Note: Copy the log4j.properties in the src location \workspace\ProjectLog4j\src\ log4j.properties .

Sunday, March 4, 2012

Clicking Compose Button in Gmail using Selenium Webdriver

In Case of Selenium Webdriver use the following code to click on compose button in Gmail:

driver.findElement(By.id("Email")).sendKeys("XXXX@gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("password");
driver.findElement(By.id("signIn")).click();

//wait for page to load
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

//this is important to click on any element on gmail page
driver.switchTo().frame("canvas_frame");
//Now , click on compose mail
driver.findElement(By.xpath("//div[contains(text(), 'Compose mail')]")).click();

==========================================================
In Case of Selenium RC use the following code to click on compose button in Gmail:

selenium.clickAt("//div[contains(text(), 'Compose mail')]","50,50");



Friday, January 27, 2012

Using xPath in Selenium

There is no one way to write an xPath statement, but there is good and bad ways.

Things to think about
• Avoid including text in xPath statement as then test won't work in a different language
• Beware of other objects in the page which may use the same attributes
• Always narrow down to a particular code block which can be uniquely identified
Example:



Option1:
//div[@id='list']/table/tbody/tr/td/div[2]/a
Option2:
//div[@id='list']/table/tbody/tr/td/div/a[contains(text(),'Welcome users who')]
Option3:
//div[@id='list']//a[contains(text(),'Welcome users who')]
Option4:
//div[@id='list']//a[contains(@href,'notify.do?user=welcome')]

Option 4 is the best way to identify the link!

Selenium Tutorial to set up a Selenium Grid

This tutorial walks you through the process of setting up Selenium Grid. I am going to first discuss the idea of Selenium Grid.

Selenium Grid is a Selenium derivative that allows the developer/tester to access Selenium Remote Controls without worrying about where the Selenium Remote Controls are. You as a developer/test create your script as you would for Selenium Remote Control and run it as normal. This is good if you want to test a bug with a specific browser but can't have it installed locally like multiple versions of Internet Explorer.

NOTE: Selenium Grid can only be controlled by Selenium Tests in a programming language. You will NOT be able to run Selenium HTML Test suites against it.

1. You will need to download and install the latest Java JRE on your system. You can find this at http://java.sun.com/ .
2. Once you have installed it, open up a command prompt and run java -version. It should look something like the image below.


3. You need to check that you have Apache Ant. If you open up a terminal/command prompt and type ant -version. If it doesnt return an error then you should be fine. If it returns an error you will need to download Apache Ant and expand the archive to a folder on harddrive.
4. You will need to now download Selenium Grid and put that in a folder that is easy to access. Now that we have all the relevant parts installed lets start the grid up.
5. We are going to start up the Selenium Grid Hub first. Open up a command prompt or terminal and navigate to the folder where the Selenium Grid files are stored.
6. From the command prompt run ant launch-hub. This will get the hub running. To see it running put http://localhost:4444/console into a browser and you will be shown a webpage with 3 columns. The first column shows what Environments it can handle, the second column shows what remote controls are available and the third column shows what remote controls are currently running tests. Like the following screenshot



7. Now that we have our hub running, lets start up a remote control. Open up another command prompt or terminal and navigate to the Selenium Grid folder. Run ant launch-remote-control. This will launch a Selenium Remote Control for handling Firefox on your OS on port 5555
8. We now have a Selenium Grid up and running! Lets run some tests against it.
9. To setup an node for Internet Explorer you need to run it as ant -Dport=5556 -Denvironment=*iexplore launch-remote-control. If you Selenium Grid node is not on the same machine as the hub just add -DhubURL= -Dhost=
10. I hope that you find this really useful. I will publish a tutorial on running tests concurrently.
One last thing, if you are running your tests on your Grid and the machine dies and you want to remove it from your grid just run http://:4444/registration-manager/unregister?host=&port=&environment= in a browser and it will unregister it from the hub. This last bit of information will make life a lot easier for you. e.g http://localhost:4444/registration-manager/unregister?host=locahost&port=5556&environment=*iexplore