Sunday 23 April 2017

How to priorities Testcases in Selenium Webdriver using TestNG

How to priorities Testcases in Selenium Webdriver using TestNG
        To priorities testcase we need to set priority to every testcase. In below example we have prioritized Login Testcase first and Location Testcase second. We have used "priority" keyword in Test annotation. Test execution starts with 0 priority.

import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class LoginLinked {
   
    FirefoxDriver driver = new FirefoxDriver();
   
    @BeforeClass
    public void BrowserIntiate()
   {       
          driver.get("https://www.linkedin.com");
          driver.manage().window().maximize();
    }
   
    @Test(priority=0) // It will execute first as its priority is zero
    public void Login()
    {
         WebElement txtUserName = driver.findElementById("login-email");
          txtUserName.sendKeys("Username");
         
          WebElement txtPassword = driver.findElementById("login-password");
          txtPassword.sendKeys("Password");
         
          WebElement btnLogIn = driver.findElementByName("submit");
          btnLogIn.click();      
         
    }
   
    @Test(priority=1)
    public void LocationTest()
   {
          WebElement lnkAdvance = driver.findElementByLinkText("Advanced");
          lnkAdvance.click();
         
          WebElement lnkClose = driver.findElementByXPath("//button[@class='text-button close-advs']");
          lnkClose.click();
         
          WebElement chkCountryIN = driver.findElementById("in:0-G-ffs");
          chkCountryIN.click();         
         
         Assert.assertEquals("INDI", chkCountryIN.getText());
    }
   
@AfterClass
    public void BrowserQuit()
   {
        driver.quit();
    }
   
}

Saturday 22 April 2017

How to Maximize window using selenium Web Driver

How to Maximize browser window using selenium Web Driver


import org.openqa.selenium.firefox.FirefoxDriver; // If you are using Firefox Browser
import org.openqa.selenium.chrome.ChromeDriver;// If you are using Chrome Browser
import org.openqa.selenium.ie.InternetExplorerDriver; // If you are using IE
import java.util.concurrent.TimeUnit;


public class MaximizeWindow {

    public static void main(String[] args)
   {
         FirefoxDriver driver = new FirefoxDriver();
         driver.get("https://www.linkedin.com");
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
         driver.manage().window().maximize();        // for maximize window           
    }

}

Wednesday 12 April 2017

Working With Popup Windows

                                                
                                             Working With Popup Windows

                                There are different type of windows in selenium
                                        
                                     1. Javascript Alert popups.
                                     2. Windows Alert popups.


 1. Javascript Alert Popup :
                                            Selenium provides an Alert class for working with javascript alerts. Method switchTo() to be used to switch driver context to the alert box before any user action can be taken it.

                   Alert alert = driver.switchTo().alert()

    Methods of Alert class :
                 
                   a. accept() :  This method clicks on the 'OK' button of the alert box.
                                       
                                        Syntax:                                                   
                                        driver.switchTo().alert().accept();

                     
                                       


                   b. dismiss() : This method clickes on the 'Cancel' button of the alert box.

                                         Syntax :
                                         driver.switchTo().alert().dismiss();
                                        
                                       
                     c. getText() : This method retrives the text displayed on the alertbox.             
                                         
                                           Syntax :
                                           driver.switchTo().alert().getText();



                    d. sendKeys() : This method will write the specified text string on the alert
                                               prompt box.
                                      
                                             Syntax :
                                             driver.switchTo().alert().sendKeys("Your Text Here");
                                           


2. Windows Alert Popups :
                                            We can handle these popus using two ways.
                                             1. Using third party Auto IT tool.
                                             2. Using Robot Class.