Automation Using Selenium Webdriver

Tuesday, 18 October 2016

Yahoo mail sign out Code Selenium WebDriver

Code for yahoo mail sign out

FirefoxDriver driver = new FirefoxDriver();
driver.get("https://login.yahoo.com/config/login_verify2?.intl=ca&.src=ym");
driver.manage().window().maximize();

WebElement element = driver.findElement(By.id("username"));
element.sendKeys("myid@yahoo.com");

driver.findElement(By.id("passwd")).sendKeys("mypassword");
element.submit();
Thread.sleep(40000);

driver.findElement(By.linkText("Sign Out")).click();  
Thread.sleep(40);

Monday, 17 October 2016

@DataProvider TestNG Real time Project Data-Driven Testing Example

 TestNG Selenium Data-Driven Testing Example
Suppose we want to do multiple searches in google by using our search method, we would want to pass in different search strings each time we call the method. In this example, I will demonstrate data-driven testing to do multiple searches in google.
We will modify our example further and introduce a parameter to our search method. Since we will be searching multiple times, we will keep the search method name generic, call it searchGoogle(). We will provide the data using a@DataProvider method called searchStrings() which returns “TestNG” and “Selenium” as the search keywords. The@Test annotation is modified to include searchGoogle as the dataProvider.

TestNGSeleniumDataDrivenSearchExample

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.ui.ExpectedCondition;

import org.openqa.selenium.support.ui.WebDriverWait;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;

import org.testng.annotations.AfterSuite;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;



@ContextConfiguration("driver_context.xml")

public class TestNGSeleniumDataDrivenSearchExample extends

        AbstractTestNGSpringContextTests {

    private WebDriver driver;



    @BeforeClass

    public void printBrowserUsed() {

        System.out.println("Driver used is: " + driver);

    }



    @Test(dataProvider = "searchStrings")

    public void searchGoogle(final String searchKey) {

        System.out.println("Search " + searchKey + " in google");

        driver.navigate().to("http://www.google.com");

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

        System.out.println("Enter " + searchKey);

        element.sendKeys(searchKey);

        System.out.println("submit");

        element.submit();

        (new WebDriverWait(driver, 10)).until(new ExpectedCondition() {

            public Boolean apply(WebDriver d) {

                return d.getTitle().toLowerCase()

                        .startsWith(searchKey.toLowerCase());

            }

        });

        System.out.println("Got " + searchKey + " results");

    }



    @DataProvider

    private Object[][] searchStrings() {

        return new Object[][] { { "TestNG" }, { "Selenium" } };

   }



    @AfterSuite

   public void quitDriver() throws Exception {
        driver.quit();

    }

OUTPUT::
Driver used is: FirefoxDriver: firefox on WINDOWS (ab3f6869-6669-4ccf-8e8f-9479f35aa790)

Search TestNG in google

Enter TestNG

submit

Got TestNG results

Search Selenium in google

Enter Selenium

submit

Got Selenium results



===============================================

TestNgSeleniumSuite

Total tests run: 2, Failures: 0, Skips: 0

===============================================


How to compare values from the list or from dropdown list in webdriver (Java)

Currently working on Selenium WebDriver and using Java for scripting.
I have stored all drop down values of db in property file and want to compare same values whether they are in UI as in DropDown options.
The visualization.txt which is in C: directory contains the below options visualizationId=Month
So How can I compare both values are matching. I need to get all the drop down options from property file i.e visualization.txt then need to check in the drop drop down in UI

public class Ex1 {
private WebDriver d;
@Test
public void testUntitled() throws Exception {
d = new FirefoxDriver();
d.get("http://register.rediff.com/commonreg/index.php?redr=http://portfolio.rediff.com/money/jsp/loginnew.jsp?redr=home");

String[] exp = {"Month", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
WebElement dropdown = d.findElement(By.id("date_mon"));
        Select select = new Select(dropdown);

        List<WebElement> options = select.getOptions();
        for(WebElement we:options)
        {
         for (int i=0; i<exp.length; i++){
             if (we.getText().equals(exp[i])){
             System.out.println("Matched");
             }
           }
         }  }}


OUTPUT:
Mateched
Mateched
Mateched
Mateched
Mateched
Mateched
Mateched
Mateched
Mateched
Mateched
Mateched
Mateched