Automation Using Selenium Webdriver

Monday, 17 October 2016

Sing in-Sing out of Facebook using selenium WebDriver


Sign in- Sign out facebook Example using selenium webdriver
public class facebook
{
public static void main(String[] args) throws InterruptedException {

WebDriver driver = new HtmlUnitDriver();
driver.get("http://www.facebook.com");
System.out.println("Title of the page "+ driver.getTitle());
WebElement username = driver.findElement(By.id("email"));
username.sendKeys("xxxxx@gmail.com");
WebElement password = driver.findElement(By.id("pass"));
password.sendKeys("xxxxx");
WebElement Signup_button = driver.findElement(By.id("loginbutton"));
Signup_button.click();
Thread.sleep(5000);
System.out.println("After login title is = " + driver.getTitle());
WebElement logOut = driver.findElement(By.id("userNavigationLabel"));
logOut.click();
Thread.sleep(5000);
WebElement signOut = driver.findElement(By.name("Log Out"));
logOut.click();

System.out.println("Logged Out Successfully!!!!!!!!!!!!!!!!!!!");
String pagetitle = driver.getTitle();
System.out.println(pagetitle);

}}

Linkedin Login-Logout Using Selenium webdrier


Linkedin Sign in-Sign out Example Using Selenium Webdriver

   public class LinkedIn
 {
    WebDriver driver = new FirefoxDriver();
    @BeforeTest
    public void setUp() throws Exception {

        String baseUrl = "http://www.linkedin.com/";  
        driver.get(baseUrl);

    }


    @Test
    public void login() throws InterruptedException
    {
        WebElement login = driver.findElement(By.id("login-email"));
        login.sendKeys("*****@gmail.com");

        WebElement pwd = driver.findElement(By.id("login-password"));
        pwd.sendKeys("*****");


        WebElement in = driver.findElement(By.name("submit"));
        in.click();

        Thread.sleep(10000);
    }


     @Test
        public void profile()  {
    // here it gives error to me : Unable to locate element
        Thread.sleep(5000);
        Actions action = new Actions(driver);
        WebElement profile = driver.findElement(By.xpath("//*[@id='img-defer-id-1-25469']"));
        action.moveToElement(profile).build().perform();
        driver.quit();
    }


}

Sunday, 16 October 2016

Fieldlevel validation in Selenium Webdriver (Java)

 Check Filedlevel Validation in Selenium Webdriver(Java)

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Validations {
public static WebDriver d;
    public static void main(String []args)throws Exception{
        d = new FirefoxDriver();
        d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        d.get("http://www.vrlbus.in/vrl2013/register_customer.aspx");
        d.findElement(By.id("FIRSTNAME")).sendKeys("#!#!#$@#!$@!$@#$%#%^#$^^&%&$%*");
        d.findElement(By.id("Button1")).click();
        String alertMessage = d.switchTo().alert().getText();
        System.out.println(alertMessage);
      if (alertMessage.equals("First name Should not contain Special Characters")){
            System.out.println("Error displayed: First name Should not contain Special Characters");
            d.switchTo().alert().dismiss();
        } else{
            System.out.println("Accepted");
        }
        d.findElement(By.id("FIRSTNAME")).sendKeys("acbcdefghijklmnopqrstuvwxyzabcdef");
        d.findElement(By.id("Button1")).click();
         if (alertMessage.equals("First name Should not contain Special Characters")){
                System.out.println("Error displayed: First name Should not contain Special Characters");
                d.switchTo().alert().dismiss();
            } else{
                System.out.println("Accepted");
            }
        d.quit();
    }  
}


Second example


public static void main(String[] args) throws Exception {
    String[] invalidChars = {"#", "!", "$", "@", "%", "^", "&"};
    String name = "acbcdefghijklmnopqrstuvwxyzab";
    d = new FirefoxDriver();
    d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    d.get("http://www.vrlbus.in/vrl2013/register_customer.aspx");
    for (String invalid : invalidChars) {
        d.findElement(By.id("FIRSTNAME")).clear();
        d.findElement(By.id("FIRSTNAME")).sendKeys(name + invalid);
        d.findElement(By.id("Button1")).click();
        String alertMessage = d.switchTo().alert().getText();
        System.out.println(invalid);
        if (alertMessage.equals("First name Should not contain Special Characters")) {
            System.out.println("Error displayed: First name Should not contain Special Characters");
            d.switchTo().alert().dismiss();
        } else {
            System.out.println("Accepted");
        }
    }
    d.findElement(By.id("FIRSTNAME")).sendKeys("acbcdefghijklmnopqrstuvwxyzabcdef");
    d.findElement(By.id("Button1")).click();
    String alertMessage = d.switchTo().alert().getText();
    if (alertMessage.equals("First name Should not contain Special Characters")) {
        System.out.println("Error displayed: First name Should not contain Special Characters");
        d.switchTo().alert().dismiss();
    } else {
        System.out.println("Accepted");
    }
    d.quit();
}

General Function To Comparing Two Strings In Selenium WebDriver TestCase

General Function To Comparing Two Strings In Selenium WebDriver TestCase

When you will use selenium webdriver to automate real worldapplications, Many times you have to compare two strings like page title, product name or any other string. And based on comparison result you have to take specific action. Simple example of such scenario Is, You are navigating on any view product page to add product In to basket. But before adding product In basket, You wants to verify product's name to check It Is correct product or not


Many time you have to compare strings In automating any single application. So What you will do? You will write samecode multiple time In your different scripts? General 
way(Which I am using) Is to create common method In baseclass (Which Is accessible from all test classes) to compare two strings. Then we can call that method by passing actualand expected string to compare strings whenever required In out tests.

Let me explain you how to do It.

Step 1 : Create CommonFunctions Class In your package which can be accessible by any other class.
Step 2 : Create method compareStrings which can accept twostring values. Also Create object of TestNG SoftAssert class to assert assertion softly as shown In bellow given 
example.
package Testng_Pack;
import org.testng.Assert;
import org.testng.asserts.SoftAssert;
public class CommonFunctions {

 SoftAssert Soft_Assert = new SoftAssert();

 public boolean compareStrings(String actualStr, String expectedStr){
  try{
   //If this assertion will fail, It will throw exception and catch block will be executed.
   Assert.assertEquals(actualStr, expectedStr);
   }catch(Throwable t){
    //This will throw soft assertion to keep continue your execution even assertion failure.
    //Use here hard assertion "Assert.fail("Failure Message String")"
 If you wants to stop your test on assertion failure.
    Soft_Assert.fail("Actual String '"+actualStr+"' And Expected String '"+expectedStr +"' Do Not Match.");
    //If Strings will not match, return false.
    return false;
   }
  //If Strings match, return true.
  return true;
 } 
}

Saturday, 15 October 2016

How To Get Height And Width Of Element In Selenium WebDriver

As you know, Every web element has Its own height and width or we can say size element. Sometimes you need to perform pixel to pixel testing as per client's requirement like each and every button's height should be x(some pixels) and width should be y(some pixels) through out the site. 

Now If you go for measuring height and width of every element manually then It Is time consuming on every test cycle. Automation Is best solution to measure height and width of elements because It Is one time exercise and then you can use It In every test cycle.
We have learnt how to get x y coordinates of element using selenium web driver In previous post. Now let us learn how to get size of element. I have prepared simple example to explain
 you how to get height and width of Image In selenium web driver. As you can see In bellow example, I have used getSize() method to get height and width of element.

package Testing_Pack;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class getElementSize {
 WebDriver driver;
 @BeforeTest
 public void setup() throws Exception {
  driver =new FirefoxDriver();     
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://any web page");
 }

 @Test
 public void getSize() throws Exception {
  //Locate element for which you wants to get height and width.
        WebElement Image = driver.findElement(By.xpath("//img[@border='0']"));
        
        //Get width of element.
        int ImageWidth = Image.getSize().getWidth();
        System.out.println("Image width Is "+ImageWidth+" pixels");
        
        //Get height of element.
        int ImageHeight = Image.getSize().getHeight();        
        System.out.println("Image height Is "+ImageHeight+" pixels");
 }
}

When you run above test In eclipse, It will print height and width of
 element In console as bellow.
OutPut::
Image width Is 212 pixels
Image height Is 191 pixels

POM(Page Factory)Advantages Example


PAGE FACTORY :


How To Set/Get Window Position And Size In Selenium WebDriver

Sometimes you need to set window position and size or get window size and position In selenium software test. Selenium webdriver software testing tool has many useful methods using which we can play with web browser window for different purpose. One of them Is window().maximize(); which we use Ineach and every webdriver software test to maximizing the window. Same way,webdriver has window size and position related Independent methods.

What Is window size and position?
Before learning how to set/get size and position, We need tounderstand It.
Window size means height and width of window.
Window position means distance of window from left side(X Coordinates) of screen and top side(Y Coordinates) of screen.
Bellow given Image describes you more about window size and
 position.





window().setSize() to set window size
We can use window().setSize() method to set the size of window In selenium webdriver software test. We can set window width to 300 and height to 500 dimensions using bellow given syntax In selenium webdriver.driver.manage().window().setSize(new Dimension(300,500));

window().getSize() to get window sizeWe can use window().getSize() method to get size of window.
Bellow given syntax will return window height using getSize().getHeight().driver.manage().window().getSize().getHeight()

Bellow given syntax will return window width using getSize().getWidth().
driver.manage().window().getSize().getWidth()window().setPosition() to set position of window
We can set window position to 50 points from left side and 200 points from top side using bellow given syntax.
driver.manage().window().setPosition(new Point(50,200));

window().getPosition() to get position of window Use window().getPosition().getX() to get window position from left side as bellow.
driver.manage().window().getPosition().getX()

To get window position from top side of screen, use window().getPosition().getY() In your software test as bellow.
driver.manage().window().getPosition().getY()

Full webdriver test to explore all above methods of webdriver window Is as bellow.

package Testing_Pack;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class WindowSizePosition {
 WebDriver driver;
 @BeforeTest
 public void setup() throws Exception {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
 }

 @Test(priority=1)
 public void setGetWinSize(){
  //WebDriver setSize method used to set window size width = 300 and height = 500.
  driver.manage().window().setSize(new Dimension(300,500));
  
  //WebDriver getSize method used to get window width and height.
  System.out.println("Window height Is -> "+driver.manage().window().getSize().getHeight());
  System.out.println("Window width Is -> "+driver.manage().window().getSize().getWidth());
 }

 @Test(priority=2)
 public void setGetWinPosition(){
  //WebDriver setPosition method used to set window position x coordinate = 50 and y coordinate =100.
  driver.manage().window().setPosition(new Point(50,200));
  
  //WebDriver getPosition method used to get window position x,y coordinates.
  System.out.println("Window position X coordinates Is -> "+driver.manage().window().getPosition().getX());
  System.out.println("Window position Y coordinates Is "+driver.manage().window().getPosition().getY());
 }
}