Saturday, May 2, 2015
Saturday, April 25, 2015
XSLT report in Selenium
XSLT: XML Stylesheet language transformation
When we execute our project using TestNg.xml, it automatically create a basic html report and testng-results.xml.XSLT is going to use this testing-result.xml and transform into better htmlreport
Steps to Generate XSLT reports
Step1: Create a project having test cases like
- Pass
- Fail
- Skip
Create 4 .java files such a way that: 2 successful, 1 skipped and 1 failed on run
testcase1: pass
package test;
import org.testng.annotations.Test;
import org.testng.annotations.Test;
public class passtc {
@Test
public void passresult()
{
System.out.println("Pass");
}
}
testcase2: pass
package test;
import org.testng.annotations.Test;
public class passtc1 {
@Test
public void show()
{
System.out.println("Testcase2 passed");
}
}
testcase3: failed
package test;
import org.testng.annotations.Test;
import org.testng.AssertJUnit;
import org.testng.Assert;
import org.testng.annotations.Test;
public class failtc {
@Test
public void failresult()
{
System.out.println("Fail");
AssertJUnit.assertEquals("Fail", "Pass");
}
}
testcase4: skipped
package test;
import org.testng.SkipException;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class skiptc {
@BeforeTest
public void intlize()
{
System.out.println("this test will skip");
}
@Test
public void skip()
{
throw new SkipException("Skipped");
}
}
After creating above testclasses your project look like below
testng.xml
Right click on the project, select "TestNG" and click on "Convert to TestNG" to create the testng.xml and place under project folder.
ANT build
Download the latest zip file from here,
Extract and keep it in a easily accessible folder and follow below steps:
Click on This and Download this apache-ant -1.9 zip File
Extract it into Any Directory of your System
I have extract it into my E:folder.You can Extract it where you want.
Configure Path:
After
Extracting Now you have to configure ant into your System.We need to
set our Envirnment variable.Before setting enviornment variable let me
show you My apache bin directory path.
Go to computer>Right click>properties>Advance systems Setting
Click on Envionment Variable
Click on new
Add a new System variable.Give the path upto your Bin folder of Apache
Now
Set path apache path into the existing Directory.First Search Path and
Click on Edit>Then edit path as ;%ANT_HOME%\bin.Now click on ok
Go to cmd prompt and Write ant -version.
Ant is installed succesfully.
build.xml and testng-results.xsl:
Click here to Download
Download testng-xslt jar https://github.com/prashanth-sams/testng-xslt-1.1.2
build.xml:
open build.xml file and give the libs path (Where you have testng-xslt jar and Selenium jars).
Execute build.xml in Command prompt
Change the directory to your project path.
Type ant
Type "ant compile"
Type "ant run"
Import testng-result.xslt
import testng-result.xslt inside xslt package.
finally type "ant makexsltreports"
After executing the above command the xslt_Reports folder will generate
inside your project directory
Open index.html
Thanks.
If you doubt please leave your comments below.
build.xml and testng-results.xsl:
Click here to Download
Download testng-xslt jar https://github.com/prashanth-sams/testng-xslt-1.1.2
build.xml:
open build.xml file and give the libs path (Where you have testng-xslt jar and Selenium jars).
Execute build.xml in Command prompt
Change the directory to your project path.
Type ant
Type "ant compile"
Type "ant run"
Import testng-result.xslt
import testng-result.xslt inside xslt package.
finally type "ant makexsltreports"
After executing the above command the xslt_Reports folder will generate
inside your project directory
Open index.html
Thanks.
If you doubt please leave your comments below.
Friday, April 17, 2015
File Upload in Selenium using Autoit
If you don't know how to setup AutoIt Click Here
Upload file in Selenium using AutoIt
Uploading of file is a four steps process:
Step 1: Identify the Windows control
Step 2: Build a AutoIt script using identified windows control
Step 3: Compile the .au3 script and convert it in to .exe file
Step 4: Call the .exe file in to the Selenium test case
Step 1: Identify the Windows control
1) Navigate to http://www.tinyupload.com/ page.
2) Click on ‘Choose file‘ button. It will open a Windows box for ‘File Upload‘.
3) Now go to your Start > All Program > AutoIt v3 and open ‘SciTE Script Editor‘. It will open an editor window where you copy the below script.
Autoit Script
$count = 0
While $count <> 10
$hdl=WinActivate("File Upload")
If $hdl <> 0 Then
ControlFocus("File Upload","","Edit1")
Sleep(500)
ControlSetText("File Upload","","Edit1","D:\test1.xml")
Sleep(500)
ControlClick("File Upload","","Button1")
Exit
EndIf
Sleep(1000)
$count=$count+1
WEnd
Steps to Write Scripts:
To identify objects, AutoIt has given us Windows Info tool, it is same like Object Spy in QTP and Element Inspector in any Browser. To open it go to Start > All Program> AutoIt v3 > AutoIt Window Info.
5) Now drag the ‘Finder Tool‘ box to the object in which you are interested.
You can see that Windows Info tool has populated all the information which is required to use the method.
Step 2: Build an AutoIt script using identified windows control
Take the information from the ‘Window Info” tool and fill in the ControlFocus method <ControlFocus ( “title”, “text”, controlID )>. For “title” we can use ‘Class’, ‘hWnd’ or ‘title’, “text” is optional and ‘controlId’ is “Edit1″ (Class name + Class Instance), so our final statement will be like these:
ControlFocus("File Upload","","Edit1")
Sleep(500)
The above script is only for File name text box.
ControlSetText: This command is used for setting the text on the edit field. ControlClick: This command is used for click action.
ControlSetText("File Upload","","Edit1","D:\test1.xml")
Sleep(500)
For clicking on Open button:
ControlClick("File Upload","","Button1")
The script in the editor looks like below.
Thats it .
After that click on tools -> Compile -> Build
Step 3: Compile the .au3 script and convert it in to .exe file
Now save the above script, if in case you have not saved the name earlier, please save it in .au3 format. The next step is to convert it in to .exe format. For that you need to right click on the .au3 file and select “Compile Script“.
Note: Make sure that you select ‘Compile Script’ as per your machine configuration. Select normal version if you are on 32 bits, and select x64 or x86 if you are on 64 bits.
Step 4: Call the .exe file in to the Selenium test case
Once you done with the compiling, it will create the ‘.exe’ file with the same name under the same folder and that ‘.exe’ file will be called in the Selenium Test Script by using the following script:
Process p=Runtime.getRuntime().exec("D:\\autoit\\upload.exe");
Source code to call .exe in selenium
package autoit;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class demoupload {
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
WebDriver driver=new FirefoxDriver();
driver.get("http://www.tinyupload.com/");
driver.manage().window().maximize();
driver.findElement(By.name("uploaded_file")).click();
Process p=Runtime.getRuntime().exec("D:\\autoit\\upload.exe");
p.waitFor();
}
}
Result :
Thursday, April 16, 2015
Autoit Setup for Selenium Webdriver
What is AutoIt
AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement andwindow/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying “runtimes” required!
In layman’s term AutoIt is just another automation tool like Selenium but unlike Selenium it is used for Desktop Automation rather Web Automation. It is a powerful tool and it just not automate desktop windows, button & form, it automates mouse movements & keystrokes too. Just like Selenium IDE, it also gives you the recording capability which generates the scripts for you to use the same script in you test.
AutoIt Features
1) Easy to learn: It is just another scripting language and very easy to use. Under help menu of AutoIt it gives you all the functions and methods you can use with detailed explanation and examples.
2) Simulates keystrokes: Where ever it is required to use keystrokes in your test, you can use this for example pressing enter on any dialog box and typing username and password on the popup which you cannot simulate with Selenium.
3) Simulate mouse movements: Like keystrokes there can be situations when you are required to simulate the mouse movements and it is the easiest way out for those situations.
4) Scripts can be compiled into standalone executable: It means that you do not require any IDE to run your scripts, you can easily convert your automation scripts into .exe files which can be run on their own.
5) Windows Management: You can expect to move, hide, show, resize, activate, close and pretty much do what you want with windows. Windows can be referenced by title, text on the window, size, position, class and even internal Win32 API handles.
6) Windows Controls: Directly get information on and interact with edit boxes, check boxes, list boxes, combos, buttons, status bars without the risk of keystrokes getting lost. Even work with controls in windows that aren’t active!
7) Detailed help file and large community-based support forums: You think of any action on windows, you will get it on help file. You face any issue or get stuck anywhere, the large group of users are there to help you.
In short, any windows, mouse & keystrokes simulation which we cannot handle with Selenium that can be handled with AutoIt. All we need to do is to use the script in Selenium which is generated with the help of AutoIt tool.
Steps to Download AutoIt v3
1) Go to AutoIt website and navigate to download page. It will display the latest version. The top most download is for AutoIt, click on it.
2) It will open up a pop up box, click on ‘Save File‘ button.
3) Once the download is complete, double click on the .exe file and it will ask for your agreement on their terms and condition. Click on ‘I Agree‘ button.
4) Complete the process and click on the ‘Finish‘ button at the end of the installation.
5) Go to your program menu and look at the AutoIt folder. If you have 32 bit system, then your folder will look like the image below.
If you have 64 bit system and during installation you have choose default x86 configuration, then your folder will look like the image below.
Recording of AutoIt script
Yes I know that you are surprised to see that AutoIt also have recording feature which helps in generating scripts automatically. But for that we need to use its extension called AutoIt Script Editor. Although script editor is also get installed with AutoIt but it comes with very limited feature.
Steps to Download AutoIt Script Editor
1) Go to AutoIt website and navigate to download page. It will display the latest version. The second download is for AutoIt Script Editor, click on it.
2) A new page will open, click on the top most link of ‘SciTE4AutoIt3.exe‘ and follow the process until the installation is finished.
Next post we will see how to handle autoit using selenium..
Thanks!!!!
Sunday, April 5, 2015
Handling Modal Dialog Window in Selenium Webdriver
In this tutorial we are going to see how to handle "Modal Dialog Window" using Selenium Webdriver
Disadvantage using Selenium
- Use Can't handle elements which is inside Modal Dialog Window.
Using Robot Class
- Using Robot Class we can handle Modal Dialog Window.
In this tutorial
- Open http://vodkabears.github.io/remodal/#
- Click on Show button
- In the Modal Dialog Window. Click on Follow button
Source Code :
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
public class robot_demo {
public static void main(String[] args) throws InterruptedException, AWTException {
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://vodkabears.github.io/remodal/#");
Thread.sleep(5000);
driver.findElement(By.linkText("Show")).click();
Thread.sleep(7000);
Robot rb=new Robot();
rb.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
rb.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
rb.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
rb.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
rb.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
rb.keyPress(KeyEvent.VK_ENTER);
}
}
Hope you like this post
Thankyou.
Friday, April 3, 2015
Robot Class in Selenium Webdriver
In this tutorial we are going to see how to use Robot class in selenium Webdriver.
What is Robot Class
Robot Class is available under java.awt package.
Using this Robot Class we can simulate keyboard events in Selenium.
Important Statements:import java.awt.Robot – Import this package prior to the script creation The package references to the Robot class in java which is required simulate keyboard and mouse events.
import java.awt.event.KeyEvent – The package allows the user to use keyPress and keyRelease events of keyboard.
Object Creation for Robot class
Robot rb =new Robot();
We create a reference variable for Robot class and instantiate it.
KeyPress and KeyRelease Events
rb.keyPress(KeyEvent.VK_D);
rb.keyRelease(KeyEvent.VK_D);
The keyPress and keyRelease methods simulate the user pressing and releasing a certain key on the keyboard respectively.
In this tutorial we are going to do:
1-Open Gmail.
2- Enter Username and password.
3- Using robot class press Enter button
Source code
import java.awt.AWTException;
import java.awt.Robot;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.awt.event.KeyEvent;
public class robot_demo {
public static void main(String[] args) throws InterruptedException, AWTException {
// TODO Auto-generated method stub
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.gmail.com");
Thread.sleep(5000);
driver.findElement(By.id("Email")).sendKeys("automationplace@gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("selenium");
Robot rb=new Robot();
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
}
}
Screenshot
Thank you.
Hope you like this post.
Monday, March 16, 2015
Sunday, March 15, 2015
Handling Scrollbar in Selenium webdriver
In this Tutorial we are going to see how to handle Scroll bar in the Web browser.
In this Example .
- Navigating to "http://automationplace.blogspot.in/"
- Scroll down the Scrollbar to down and Click on "OlderPost" link
- Assert the Actual and Expected URL's
The below Code is used to Scroll the Scrollbar down
EventFiringWebDriver
eventFiringWebDriver = new
EventFiringWebDriver(webDriver);
eventFiringWebDriver.executeScript("scroll(0,4000)",args);Source code:
import org.junit.Assert;
import
org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import
org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.openqa.selenium.support.events.EventFiringWebDriver;
public class testscroll {
public static void main(String[] args) throws
InterruptedException {
// TODO Auto-generated
method stub
WebDriver
webDriver = new FirefoxDriver();
webDriver.get("http://automationplace.blogspot.in/");
webDriver.manage().window().maximize();
EventFiringWebDriver
eventFiringWebDriver = new EventFiringWebDriver(webDriver);
eventFiringWebDriver.executeScript("scroll(0,4000)",args);
webDriver.findElement(By.id("Blog1_blog-pager-older-link")).click();
Thread.sleep(8000);
String
actual="http://automationplace.blogspot.in/search?updated-max=2015-02-16T18:02:00-08:00&max-results=7";
Thread.sleep(5000);
String
expectedurl=webDriver.getCurrentUrl();
Assert.assertEquals(expectedurl,
actual);
System.out.println("result is
correct");
}
}
Thanks,
Hope you like this post.
Subscribe to:
Posts (Atom)