TestNG Simple
Search Example
Selenium provides as its API for automating
web application testing. drives
the browser directly using each browser’s built-in support for automation.
In this example, we will use
the to open google and search for TestNG.
is an interface so we still need
to an implementation to run the test on an actual browser. We will select the
implementation based on the browser used.
For example, if we are
using firefox, we will use .
If it is chrome, we will use .
Since works with Chrome through the chromedriver.exe, we need
to make sure that the binary be placed somewhere on your system’s path.
package Others;
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.ExpectedCondition;
import
org.openqa.selenium.support.ui.WebDriverWait;
import
org.testng.annotations.AfterSuite;
import
org.testng.annotations.BeforeSuite;
import
org.testng.annotations.Test;
public class TestNG_Sample {
private WebDriver driver;
@BeforeSuite
public void initDriver() throws Exception {
System.out.println("You are
testing in Chrome");
System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
}
@Test
public void
searchTestNGInGoogle() {
final String searchKey = "TestNG";
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<Object>() {
public Boolean
apply(WebDriver d) {
return d.getTitle().toLowerCase()
.startsWith(searchKey.toLowerCase());
}
});
System.out.println("Got " + searchKey + "
results");
}
@AfterSuite
public void quitDriver() throws Exception {
driver.quit();
}
}
testngSimpleSearch.xml:
<?xml version="1.0"
encoding="UTF-8"?>
<suite name="TestNGSuite"
parallel="false">
<test name="TestNGTest">
<classes>
<class name="Others.TestNG_Sample" />
</classes>
</test>
</suite>
Output:
You are testing in Chrome
Search TestNG in google
Enter TestNG
submit
Got TestNG results
No comments:
Post a Comment