Showing posts with label QA. Show all posts
Showing posts with label QA. Show all posts

Wednesday, 10 December 2014

TestNG Framework in Selenium Webdriver Part 2(Annotations)

In my last blog on TestNG, we had covered installation of TestNG and did some setup to work on our projects. Now lets see about annotations available in TestNG.


Annotations Available In TestNG

@Test
This is the test annotation, used for every test method. Means, before every test method, we need to give this annotation.

@BeforeSuite
It will run before execution of test suite. The method under this annotation will execute before starting execution of any of the testcases of that testsuite. Means, it will executed first in the java file.

@AfterSuite
It will run after execution of test suite. The method under this annotation will execute after execution of all the testcases of that testsuite. Means, it will be executed at the end, after running other methods under other annotations.

@BeforeTest 
It will run before the testcase. The method under this annotation will execute before execution of test method.
And this will be executed before @BeforeClass annotation. 

@AfterTest
It will run after the testcase. The method under this annotation will execute after execution of test method.
And this will be executed after @AfterClass annotation.

@BeforeMethod 
It will run before each test method. The method under this annotation will execute before execution of   each and every test method. Means, if there are 'n' test methods, then it will execute n times i.e. before every test method.

@AfterMethod
It will run after each test method. The method under this annotation will execute after execution of   each and every test method. Means, if there are 'n' test methods, then it will execute n times i.e. after every test method.

@BeforeClass 
It will run before execution of 1st test method in the class. 

@AfterClass
It will run after execution of all test methods in the class. 

@BeforeGroup 
It will run before execution of 1st testcase of the group.

@AfterGroup
It will run after all testcases get executed of that group.

@DataProvider 
It will be used, when we want to fetch/read data from excel sheet and use in testcase.

Lets see one example with having most of the annotations in one class and there we can analyze the execution priority.

 package testOne;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestClass{

 @BeforeClass
 public static void beforeclass(){
  System.out.println("Executed before class");
 }
 
 @BeforeSuite
 public static void beforesuite(){
  System.out.println("Executed before suite");
 }
 
 @BeforeTest
 public static void beforetest(){
  System.out.println("Executed before test");
 }
 
 @BeforeMethod
 public static void beforemethod(){
  System.out.println("Executed before method");
 }

 @Test
 public static void testMethod1(){
  System.out.println("1st test case executed.");
 }
 
 @Test
 public static void testMethod2(){
  System.out.println("2nd test case executed");
 }
 
 @AfterMethod
 public static void aftermethod(){
  System.out.println("Executed after method");
 }
 
 @AfterTest
 public static void aftertest(){
  System.out.println("Executed after test");
 }
 
 @AfterSuite
 public static void aftersuite(){
  System.out.println("Executed after suite");
 }
 
 @AfterClass
 public static void afterclass(){
  System.out.println("Executed after class");
 }

}
Now do Rightclick on the class and do run as TestNG Test. Let see the output generated in console below.




Here we can see two testmethods were highlighted in screenshot. And as we saw in definition, @BeforeMethod & @AfterMethod, both were executed twice as we have twotest methods. And we can see the rest methods execution order as per annotation. 
Execution doesn't occur as per order of annotation present in coding. It follows its own order of execution as we see in the output.
In next blog we will see about assertion types and use with practical example using TestNG.

Sunday, 12 October 2014

Selenium Webdriver commands to Initialize Browser like Firefox, Safari, IE, Opera and Run

Command for Firefox/Safari driver

In my earlier blogs we have already used some commands to initialize the firefox driver.
Here is the command to initialize Firefox driver.
WebDriver driver = new FirefoxDriver();

For Safari also, we don't need to do much things. There also single line coding will work. But will work good with Mac Osx. Here is the command for safari browser.
WebDriver driver = new SafariDriver();

Like we did for Firefox/Safari driver, same will not work directly for other browsers like Chrome, Opera, Internet Explorer etc. We need few additional stuffs there.

We need to download the driver files first for other browsers. Lets start with IE browser first.

Steps and command for IE driver

Step 1: Go to this URL http://www.seleniumhq.org/download/
Step 2: Go to “The Internet Explorer Driver Server” section.


Step 3: Now you can download the IE driver zip file of 32 or 64 bit as per requirement.
Step 4: Extract the zip file and save the 'IEDriverServer.exe' in a proper place (say here: C:\\Users\\debasisa\\Desktop\\IEDriverServer.exe )


Now, here is the code to initialize the IE driver:

public static void main(String[] args){

File file = new File("C:\\Users\\debasisa\\Desktop\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("Testuser");
driver.findElement(By.id("Passwd")).sendKeys("Testpassword");
driver.findElement(By.id("signIn")).click();

driver.close();
driver.quit();

}


Explanation:
1. In first line, we created file object which holds the path for the IE driver.
2. In 2nd line, we set the property, so that Webdriver can access that path.
3. In 3rd line we initialize the IE driver.
4. Now Gmail instance is opened in IE browser. Then located the Email/Password fields through locator 'id'. Then though 'sendKeys', we entered values to Email/password fields and clicked on “SignIn” button. After that closed the IE browser.

*Note:
It will not work on all versions on IE. Supported versions are from IE 6 to IE 11, where as for Version 11, we need some additional settings in Registry file.


Steps for Additional setting to run in IE11:

Step 1: Go to Start menu of Windows system, click Run and type 'REGEDIT'
Step 2: It will open registry edit window.
Step 3: There go to the below folder path.

For 64 bit machine HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\InternetExplorer\Main\FeatureControl\
FEATURE_BFCACHE

For 32 bit machine
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InternetExplorer\Main\FeatureControl\FEATURE_BFCACHE

The last folder mentioned 'FEATURE_BFCACHE' might not present there. So, create a new Key there with this name.



Step 4: Now in that 'FEATURE_BFCACHE' folder, create a 'DWORD' with name: 'iexplore.exe' & value 0 .



Step 5: Now we are all set. We can run our same piece of code in Eclipse, it will work in IE 11 as well.


*Note:
But running code in IE 11 is bit slower. Through sendkeys when we pass values like Email/Password etc., we can see that it will type very slowly. And it is because of 64bit driver.

Try using 32 bit IE driver file. Though it is not much compatible with Win 64 bit system, but this slow responsiveness issue can be resolved by this 32 bit IE driver server.

We need all these additional settings, because IE driver server doesn't support directly for IE 11.
But currently IE 11 driver server is available to download, but that is for Win8.
If you are on Win8, it is worth trying.
You can download IE 11 driver server from here.


Steps and command for Chrome driver

Step 1: Go to this URL http://www.seleniumhq.org/download/
Step 2: Go to “Third Party Drivers, Bindings, and Plugins” section.



Step 3: Click on the version no. and then choose the link as per need like Win/Mac/Linux etc. And then download the zip file.
Step4: Extract and save the “chromedriver.exe” file in a proper place (say here: C:\\Users\\debasisa\\Desktop\\chromedriver.exe )


Now, here is the code to initialize the Chrome driver:

public static void main(String[] args){

File file = new File("C:\\Users\\debasisa\\Desktop\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver = new ChromeDriver();
driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("Testuser");
driver.findElement(By.id("Passwd")).sendKeys("Testpassword");
driver.findElement(By.id("signIn")).click();

driver.close();
driver.quit();

}

Explanation:
1. In first line, we created file object which holds the path for the Chrome driver. 
2. In 2nd line, we set the property, so that Webdriver can access that path.
3. In 3rd line we initialize the Chrome driver.
4. Now Gmail instance is opened in Chrome browser. Then located the Email/Password fields through locator 'id'. Then though 'sendKeys', we entered values to Email/password fields and clicked on “SignIn” button. After that closed the chrome browser.




Steps and command for Opera driver
Like we downloaded the driver file for Chrome, we can download the opera driver file in the same way.

But here after extract, we will get a .jar file. We need to add that jar file to build path.
Here are the steps below.

Step 1: Go to this URL http://www.seleniumhq.org/download/
Step 2: Go to “Third Party Drivers, Bindings, and Plugins” section.

Step 3: Click on the version no. to download. Here, it will download the .jar file, rather than .exe file.
Step 4: Now, we need to add that jar file to the build path of our project in Eclipse.
Here are the steps mentioned below to add the jar file.

Open Eclipse → Go to Project → Right click → Build Path → Configure Build Path → Add External JARs → Then select the downloaded Opera driver jar file → Click Ok to save that

After adding jar file in build path, we can view the same.

                             


Step 5: Now copy the path of 'opera.exe' file. It is the launcher file, what we got after installation of opera browser, mostly the location is in program files. 

Say the path is: “C:\\Program Files (x86)\\Opera\\opera.exe” .

Now here is the code to initialize opera driver and run the program in opera browser.



public static void main(String[] args){

File file= new File("C:\\Program Files (x86)\\Opera\\opera.exe");
System.setProperty("webdriver.opera.driver", file.getAbsolutePath());
WebDriver driver = new OperaDriver();

driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("Testuser");
driver.findElement(By.id("Passwd")).sendKeys("Testpassword");
driver.findElement(By.id("signIn")).click();

driver.close();
driver.quit();

}

Explanation:

1. In first line, we created file object which holds the path for the Opera browser. Here it is the path of 'opera.exe' which we saved in step 5 above.
2. In 2nd line, we set the property, so that Webdriver can access that path.
3. In 3rd line we initialize the Opera driver.
4. Now Gmail instance is opened in Opera browser. Then located the Email/Password fields through locator 'id'. Then though 'sendKeys', we entered values to Email/password fields and clicked on “SignIn” button. After that closed the opera browser.


*Note:
It will work only for opera versions 12.x or lower than that. For higher versions of opera, it won't work.


Monday, 22 September 2014

Locating Web Elements in Selenium Webdriver PART 2

How to Locate Web Elements By Xpath?

In my last blog, we discussed about locating web elements using Id. But there was a condition, it can be done only if that particular element has Id.
So, we need some other ways like xpath/css selectors to locate the elements.

We already discussed about using Firebug to get the html text and there we found the Id of that particular Element.
Here is the same piece of html code.

<input
id="Email" class="" type="email" spellcheck="false" value="" placeholder="Email" name="Email">

Here we can write xpath like: “//input[@id='Email']

So, the code will look like below.

driver.findElement(By.xpath("//input[@id='Email']"));

So, this is the manual way to write xpath. But there are several ways to get xpath as well.

Steps to Find Xpath:
Step 1: Install Firepath, which is an Add-on for firefox browser.
Step 2: Open the testing site in Firefox (say we have opened “https:// www.gmail.com”)
Step 3: Launch Firebug, go to Firepath tab and start inspecting elements.
Step 4: Move mouse to the field/button/link for which you want to locate, then click (say 'Email' field)
Step 5: In Firepath, we can see the xpath for that particular field.

See the screen shot below.


Here we found xpath as “//*[@id='Email']

* Note: Exclude the beginning dot(.) while copying the xpath.


Now our piece of code will look like below.
driver.findElement(By.xpath("//[@id='Email']"));

Here the xpath we found is partial xpath where as the earlier one what we have written manually is full xpath.

Another way of getting xpath: Through Firebug when we inspect element, we can get the html code.There we can right click and copy the xpath as well.

See the screen shot below.





So, now we can use our earlier xpath which we wrote manually above OR copied through Firebug OR found though Firepath .

Some times the generated xpath may not work, there we need to manually modify that. Also in some cases, if the locator is not detectable, then we can record the xpath from Selenium IDE (Firefox plugin) and put that in our code.

Steps to find Xpath using Selenium IDE:
1. Open Selenium IDE (Its a Firefox Plugin to record the scenario and run those testcases)
2. Enter the base URL (Say “https://gmail.com”)
3. Click on Record button and start browsing in firefox browser. (Open Gmail sign in page, Enter data in email field)
4. Now the same is recorded and viewed in IDE.
5. Select that row and go to the target drop-down.
6. In the target drop-down, we can see the id/xpath/css selectors etc. of that particular element.

See the screen shot below.




Now lets see the code to Enter Email/Password and click submit button in Gmail using xpath.

Public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com/");
driver.findElement(By.xpath("//[@id='Email']")).sendKeys("Testuser");
driver.findElement(By.xpath("//[@id='Passwd']")).sendKeys("Testpassword");
driver.findElement(By.xpath("//[@id='signIn']")).click();
driver.close();
driver.quit();

}

In the above code, we opened Gmail sign in page in Firefox browser.
Then located the Email/Password fields through locator 'Xpath'.
Then though 'sendKeys', we entered values to Email/password fields and clicked on “SignIn” button. After that closed the browser.

In next blog, we will see for cssSelector and other locators as well.