Showing posts with label Selenium Webdriver. Show all posts
Showing posts with label Selenium Webdriver. 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.

Tuesday, 9 December 2014

TestNG Framework in Selenium Webdriver Part 1

What is TestNG?

TestNG is a selenium testing framework, by using which we can run our test cases/test suites in any order we need. There is one more framework available i.e. JUnit. But TestNG is more powerful as compared to JUnit as it is designed to overcome the limitations of JUnit and it supports lot more things as compared to JUnit.
We will see in details for this later.
Lets move on for TestNG and how can we use this and what are the annotations available in TestNG.

Installation of TestNG in Eclipse

Step 1: Open Eclipse and go to Help menu
Step 2: Click on 'Install New Software'. It will show the install window.
Step 3: There put the url:  http://beust.com/eclipse and then click Add. After adding it will show 'TestNG' in the list.
Step 4: Select TestNG checkbox and click Next.




Step 5: Then proceed and click Finish at the end.

There is another way present to install TestNG also. Please look at below steps.

Step 1: In Eclipse >Help menu, we can find 'Eclipse Marketplace...'. Click there.
Step 2: It will open a window. There type 'TestNG' and search. It will show that software to install.

Step 3: Click on Install there. Instead of Install, Uninstall appear here as it is already installed in my machine. After clicking install proceed as per the instruction shown over there.

Setup to use in project

1. Need to include webdriver jar files after creating a project. For more info regarding webdriver jar files, can visit my earlier blog on webdriver jar files..
2. Apart from this, we need TestNG library. Go to below path and include this library to your project.
Right click on Project > Build Path > Configure Build Path > Libraries > Add Library > Select TestNG >Next > Finish



3. Now you can create class file and do your coding stuff. Then to run that program using TestNG, we can right click > Run as, and then select TestNG there.




Now we are done with installation and setup for TestNG in Eclipse. This is just an introduction on how to start with TestNG.
In next blog, we will see all the annotations available in TestNG with practical example and then run them using TestNG.

Tuesday, 11 November 2014

Wait commands in Selenium Webdriver

While executing our scripts for websites, some times wait command is needed. Like, sometimes we need to wait for an element to load, then only we can perform task on the same.
Selenium webdriver provides 2 types of wait commands: Implicit Wait & Explicit Wait. And it is always good to prefer implicit wait over explicit wait.

Implicit Wait:
Here in implicit wait, we need to write some wait command and set some time, so that it will wait for that amount of time for that element to load. And as it is implicit wait, it will be applicable to all elements in webdriver.

Sometimes, if we don't use use any wait command, then our webdriver script may get failed. It is because, the element on which we are about to perform action is not found by the webdriver locators as that element takes some time to load on the web page.

Here is the Implicit wait command below.
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

In this command, webdriver will wait for 20 seconds for every element, if that element doesn't appear on webpage.

Let see an example here.
public static void main(String[] args){
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.id("Email")).sendKeys("testuser1@gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("xyz");
}

Here in the above code, we have set implicit time as 20 seconds. And we have two elements here: Email & Password. So, here webdriver will wait for 20 seconds, if the element is not found. After 20 seconds, if it is found, it will proceed else will throw error.


Explicit Wait:
As we discussed earlier, Implicit wait will be applicable to all web elements present. But Explicit will be applicable for the element we choose.
Here the command will look like below.

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.id("Email")));

Lets see an example here.

public static void main(String[] args){
WebDriver driver = new FirefoxDriver();
driver.findElement(By.id("Email")).sendKeys("testuser1@gmail.com"); 
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.id("Passwd")));
driver.findElement(By.id("Passwd")).sendKeys("xyz");
}

Here, for Email field, there is no wait implemented. But for Password field, webdriver will wait for 20 seconds until that element is clickable. And once element is found clickable, then it will enter the password.

Instead of "elementToBeClickable", we can use "presenceOfElementLocated" as well.
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("passwd")));
Here, it will wait until the password field is located. Then it will perform the next action on that web element.

Friday, 17 October 2014

Few useful Commands in Selenium Webdriver

Open Browser
WebDriver driver = new FirefoxDriver();
For other browser and details are discussed in my earlier blog. Please have a look here.

Open Page URL
Get” method is used to open a particular url in browser.
driver.get("https://www.gmail.com");

Passing values in text field
sendKeys” keyword is used to enter values to the field.
driver.findElement(By.id("Email")).sendKeys("testuser@gmail.com");


Clear value from Text field
clear() method is used to clear the field.
driver.findElement(By.id("Email")).clear();

Clicking button
Here click() method is used to click a button.
driver.findElement(By.id("signIn")).click();


Submit button
In the above case, Sign in is a button. But works as a submit button too. So, below code will work as well.
driver.findElement(By.id("signIn")).submit();

Selecting value from drop-down
We have 3 different methods to select the drop-down value.
selectByIndex() → Here index is the order in drop-down, and 1st element is always having index 0.
selectByVisibleText() → It is the visible text , what we see in drop-down list and want to select.
selectByValue() → It is the option value, which can be viewed in Firebug inspector

Look at these option values, we got through Firebug inspector for that particular dropdown.

<option value="">Select Country</option>
<option value="111">Denmark</option>
<option value="120">Sweden</option>
<option value="127">Normay</option>
<option value="139">Germany</option>
<option value="143">Paris</option>
<option value="219">Austria</option>
<option value="224">Spain</option>


Let see an example here of selecting value by 3 ways.

Select selectBox = new Select(driver.findElement(By.id("Country")); // Here dropdown field is clicked
selectBox.selectByIndex(0); // Select through Index
“OR”
selectBox.selectByVisibleText(“Denmark”); //Select through visible Text
“OR”
selectBox.selectByValue(111); //Select through option value

Deselecting value from drop-down
Here also we have 3 different methods to select the drop-down value.
deselectByIndex() → Here index is the order in drop-down, and 1st element is always having index 0.
deselectByVisibleText() → It is the visible text , what we see in drop-down list and want to select.
deselectByValue() → It is the option value, which can be viewed in Firebug inspector

Here command is also almost same as selecting drop-down.
selectBox.deselectByIndex(0); // Deselect through Index
“OR”
selectBox.deselectByVisibleText(“Denmark”); //Deselect through visible Text
“OR”
selectBox.deselectByValue(111); //Deselect through option value

Selecting/Deselecting Check box
Here click() method is used to toggle on/off for checkbox.
driver.findElement(By.id("PersistentCookie")).click();


Selecting Radio button
Here also, it will work in same way of checkbox.
driver.findElement(By.id("Ad_Type_1")).click();

Accepting a Confirm box
driver.findElement(By.id(("Delete_Ad"))).click(); // Here the Alert window is opened on clicking   button
Alert alert = driver.switchTo().alert(); 
alert.accept(); // This line is to accept the alert box, means clicking Ok.


For Alert and Prompt boxes also, It will work in same way.

Rejecting a Confirm box
Here, also it will work in same way as of Accepting confirm box.
driver.findElement(By.id(("Delete_Ad"))).click(); // Here the Alert window is opened on clicking   button
Alert alert = driver.switchTo().alert();
alert.dismiss();  // This line is to click 'Cancel' in the confirm box.

For Prompt boxes also, it will work in same way.
And in Alert box, we don't have cancel button to reject.

Maximize screen
Here is the command to maximize the brower window.
driver.manage().window().maximize();

Navigate
Here are few commands related to navigation.
driver.navigate().to("http://thetestzing.blogspot.in//2014/10/locating-web-elements-in-selenium.html"); // Here it will navigate to the particular page.
driver.navigate().back(); // Here it is same as clicking browser back button
driver.navigate().forward(); // Here it is same as clicking browser forward button
driver.navigate().refresh(); // Here it is same as refreshing/reloading the browser page.

Verify Element
Sometimes in our application, we may need some verification. Like after logging into a site, we can verify whether the login name present or not and we can confirm that correct user is logged in.

Lets see the example below.

WebElement Element = driver.findElement(By.xpath("//*[@id='Text1']/div[1]/span"));

Boolean iselementpresent = Element.isEnabled();
if (iselementpresent == true) {
System.out.println("Text is--"+ Element.getText());
}
else{
System.out.println("Element not found");
}
Here in the 1st line we found the xpath for a text element and stored that path in a web element.
Then created one boolean object which will accept true or false value.
Then in If-Else block we compared that boolean value. If it is true, then it will print that text, else will print the message mentioned.