Single Post

Header

Sunday, November 1, 2015

AssertTrue and AssertEquals in Selenium WebDriver

Examples for AssertTrue and AssertEquals using Selenium WebDriver

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import org.apache.commons.io.FileUtils;

public class AssertTrueAssertEquals {
   
    @Test
    public void asserttrue() throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.co.in");
        driver.manage().window().maximize();
        Thread.sleep(2000);
        driver.findElement(By.
className("gsfi")).sendKeys("testing");
        driver.findElement(By.name("btnG")).click();
        Thread.sleep(3000);
        WebElement resultsLabel = driver.findElement(By.id("resultStats"));
        assertTrue(resultsLabel.isDisplayed());

        driver.close();
    }
   
    @Test
    public void assertequals() throws InterruptedException, IOException {
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.co.in");
        driver.manage().window().maximize();
        Thread.sleep(2000);
        driver.findElement(By.
className("gsfi")).sendKeys("abcdefghijkl1234324254");
        driver.findElement(By.name("btnG")).click();
        Thread.sleep(3000);
        // take the screenshot
        File scrFile = ((TakesScreenshot) driver)
                .getScreenshotAs(OutputType.FILE);
        // now save the screenshot to a file some place
        FileUtils.copyFile(scrFile, new File("e:\\tmp\\screenshot.png"));
        WebElement resultsLabel = driver.findElement(By.id("resultStats"));
        assertEquals(true, resultsLabel.isDisplayed());

        driver.close(); // This will not execute if the above assert fails otherwise it will execute.
    }
   
}

No comments:

Post a Comment