About Me

Page views

Powered by Blogger.

Followers

Thursday 30 April 2015


All Links can be accessed using an exact or partial match of their link text. 
The scenarios where multiple matches would exist, and would explain how WebDriver would deal with all Links .

The common procedures in web  testing is to test if all the links present within the page are working. This can be conveniently done using a combination of the Java for-each loop and the By.tagName("a") method. Tthe Mercury Tours homepage to determine those that are working and those that are still under construction.



package dayOne;

import java.util.List; import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestLinks {

    public static void main(String[] args) {
        String baseUrl = "http://newtours.demoaut.com/";
        WebDriver driver = new FirefoxDriver();
        String underConsTitle = "Under Construction: Mercury Tours";
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        driver.get(baseUrl);
        List<WebElement> linkElements = driver.findElements(By.tagName("a"));
        String[] linkTexts = new String[linkElements.size()];
        int i = 0;

        //extract the link texts of each link element
        for (WebElement e : linkElements) {
            linkTexts[i] = e.getText();
            i++;
        }

        //test each link
        for (String t : linkTexts) {
            driver.findElement(By.linkText(t)).click();
            if (driver.getTitle().equals(underConsTitle)) {
                System.out.println("\"" + t + "\""
                        + " is under construction.");
            } else {
                System.out.println("\"" + t + "\""
                        + " is working.");
            }
            driver.navigate().back();
        }
       // driver.quit();
    }

}

Result :





Click on Facebook's "Keep me logged in" check box twice and then output the result as TRUE when it is toggled on, and FALSE if it is toggled off
1. First click check box was toggled on .
2. Second click check box was toggled off.


package dayOne;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FacebookChkbox {


public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

String baseUrl = "https://www.facebook.com/";

driver.get(baseUrl);

WebElement checkFBPersist = driver.findElement(By.id("persist_box"));

for (int i = 0; i<2; i++){

checkFBPersist.click();

System.out.println(checkFBPersist.isSelected());
}

driver.quit();
}

}





package dayOne;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class JobServe {


public static void main(String[] args) {

 WebDriver driver = new FirefoxDriver();

 driver.get("http://www.jobserve.com/in/en/Job-Search/");

 driver.findElement(By.xpath("/html/body/form/div[4]/div[1]/div[12]/div[1]/div/div[2]/div[2]/div/div[1]/div[2]/span/span")).click();

 driver.findElement(By.xpath("/html/body/form/div[4]/div[1]/div[12]/div[1]/div/div[2]/div[2]/div/div[1]/div[2]/div/div/div[1]/input")).click();

 int selection = 5;

String xpath_start = "/html/body/form/div[4]/div[1]/div[12]/div[1]/div/div[2]/div[2]/div/div[1]/div[2]/div/div/div[";
String xpath_end = "]/input";

driver.findElement(By.xpath(xpath_start + selection + xpath_end )).click(); 
}

}





WebDriver gives us “Find Element” and “Find Elements” method to locate elements on the web page.


By Name

WebElement element = driver.findElement(By.name("ss"));

By ID

WebElement element = driver.findElement(By.id("Username"));


By Class Name

WebElement element = driver.findElement(By.className(""));


By Link Text


WebElement element=driver.findElement(By.linkText("Link Name"));


By Tag Name

WebElement element=driver.findElement(By.tagName("dt"));


By Partial Link Text


WebElement element=driver.findElement(By.partialLinkText("Link"));


Practice Yourself :


package dayOne;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebElementsCommands {

public static WebDriver driver;
public static void main(String[] args) {

driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.toolsqa.com/Automation-practice-form/");
driver.findElement(By.partialLinkText("Partial Link Test")).click();
 
System.out.println("Link Test Pass");
String sClass = driver.findElements(By.tagName("button")).toString();
System.out.println(sClass);
driver.findElement(By.linkText("Link Test")).click();
System.out.println(" Test Pass");
}

}

Wednesday 29 April 2015


To Command

driver.navigate().to("https://in.yahoo.com/");

Purpose : This command is use to navigate on specific page or URL in between the  test Command : driver.navigate().to(URL); Parameters : url – The URL to load. It is best to use a fully qualified URL

Forward Command

driver.navigate().forward();


Purpose : This command is use to go on to next page like browser’s forward button.

Back Command

driver.navigate().back();

Purpose : This command is use to go back to previous page like browser’s back button.

Refresh Command

driver.navigate().refresh();

Purpose : This command is use to refresh the current page.

Practice Yourself


package dayOne;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NavigationCommands {
private static WebDriver driver = null;
public static void main(String[] args) {
// Create a new instance of the Firefox driver
driver = new FirefoxDriver();
// Open ToolsQA website
driver.get("http://www.bbc.com/");
// Put an Implicit wait on driver
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Click on About link
        driver.findElement(By.linkText("Earth")).click();
// Go back to Home Page
driver.navigate().back();
// Go forward to About page
        driver.navigate().forward();
         // Go back to Home page
        driver.navigate().to("http://www.bbc.com/");
  // Refresh browser
  driver.navigate().refresh();
  // Close browser
  driver.close();

}
}


Get Command

Example : driver.get("http://www.bbc.com");

Purpose: This command is use to open a new web page in the current browser.
Command: driver.get("URL");


Get Title Command

Example : driver.getTitle();
Purpose: This command is use to get the title of the current page.


Get Current URL Command

Example : driver.getCurrentUrl()
Purpose: This command is use to get the URL of the page currently loaded in the browser.


Close Command

Example : driver.close();
Purpose: This command is use to close the current window of the browser, if it’s the last window it will close the browser.

Quit Command

Example : driver.quit();
Purpose : This command is use to quit the browser and all the opened windows in the browser.


Get Page Source Command

Example : driver.getPageSource();
Purpose: This command is use to get the source of the last loaded page.


Refresh Command

Example : driver.navigate().refresh();
Purpose: This command is use to refresh the current browser.




package dayOne;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestTwo {


public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

String baseUrl = "http://enterprise.demo.orangehrmlive.com/";

StringBuffer expectedTitle = new StringBuffer("OrangeHRM");

String actualTitle = "";

driver.get(baseUrl);

actualTitle = driver.getTitle();

    if (actualTitle.contentEquals(expectedTitle)){
            System.out.println("Test Passed!");
            } else {
            System.out.println("Test Failed");
        }
driver.close();
}

}








Code Explanation :  


  1. org.openqa.selenium.*-  The WebDriver class needed to instantiate a new browser loaded with a specific driver .
  2. org.openqa.selenium.firefox.FirefoxDriver - The FirefoxDriver class needed to instantiate a Firefox-specific driver onto the browser instantiated by the WebDriver class
  3. WebDriver driver = new FirefoxDriver();  - Instantiating objects and variables
  4. driver.get(baseUrl); - Launching a Browser Session.

  5. actualTitle = driver.getTitle(); - Get the Actual Page Title.


  6. if (actualTitle.contentEquals(expectedTitle)){

                System.out.println("Test Passed!");
            } else {
                System.out.println("Test Failed");
            }                                                          

    Compare the Expected and Actual Values.

  7. driver.close(); - Terminating a Browser Session.



Popular Posts

Copyright © Learn Selenium Yourself | Powered by Blogger
Design by Duan Zhiyan | Blogger Theme by NewBloggerThemes.com