testng.tests.tests.java Source code

Java tutorial

Introduction

Here is the source code for testng.tests.tests.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testng.tests;

/**
 *
 * @author micro
 */
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import static org.testng.Assert.*;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

/**
 *
 * @author micro
 */
public class tests {

    private WebDriver driver;

    public tests() {
    }

    @BeforeTest
    public void setUpTest() throws Exception {
        System.setProperty("webdriver.chrome.driver", "//usr//share//rats//chromedriver");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        driver.get("http://www.amazon.co.uk");

    }

    @AfterTest
    public void tearDownClass() throws Exception {
        driver.close();

    }

    @BeforeMethod
    public void setUpMethod() throws Exception {

    }

    @AfterMethod
    public void tearDownMethod() throws Exception {
        driver.navigate().to("http://www.amazon.co.uk");
    }

    /**
     * Test of main method, of class Seltest.
     */
    @Test(priority = 0)
    public void testSuccess() {
        WebElement we = driver.findElement(By.xpath("//*[@id='searchDropdownBox']"));
        Select sel = new Select(we);
        sel.selectByVisibleText("Books");
        WebElement web = driver.findElement(By.xpath("//*[@id='twotabsearchtextbox']"));
        web.sendKeys("Selenium");
        WebElement webe = driver.findElement(By.xpath("//*[@id='nav-search']/form/div[2]/div/input"));
        webe.click();
        String pageTitle = driver.getTitle();
        assertEquals("Amazon.co.uk: Selenium: Books", pageTitle);

    }

    @Test(priority = 1)
    public void testFail() {
        WebElement we = driver.findElement(By.xpath("//*[@id='searchDropdownBox']"));
        Select sel = new Select(we);
        sel.selectByVisibleText("Books");
        WebElement web = driver.findElement(By.xpath("//*[@id='twotabsearchtextbox']"));
        web.sendKeys("Selenium");
        WebElement webe = driver.findElement(By.xpath("//*[@id='nav-search']/form/div[2]/div/input"));
        webe.click();
        String pageTitle = driver.getTitle();
        assertEquals("Amazon.co: Selenium: Books", pageTitle);

    }

}