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");