Test


import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class demo
{
private WebDriver driver;

@DataProvider
public Object[][] dataObjects()
{
Object[][] data = { { "https://www.baidu.com/", "selenium" } };
return data;
}

@BeforeMethod(alwaysRun = true)
public void beforeTest()
{
FirefoxProfile profile = new FirefoxProfile();
driver = new FirefoxDriver(profile);
}

@Test(groups = "Search", dataProvider = "dataObjects")
public void LoginTest(String base_url, String kw) throws InterruptedException
{
driver.manage().window().maximize();
driver.get(base_url);
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
System.out.println(driver.getCurrentUrl());
WebElement inputBox = driver.findElement(By.id("kw"));
type(inputBox, kw);
WebElement searchButton = (new WebDriverWait(driver, 60)).until(new ExpectedCondition<WebElement>()
{
@Override
public WebElement apply(WebDriver d)
{
return d.findElement((By.cssSelector("input#su")));
}
});

searchButton.submit();
Thread.sleep(6000);
Assert.assertTrue(driver.getTitle().contains(kw));

}

@AfterMethod(alwaysRun = true)
public void afterTearDown()
{
driver.quit();
}

/**
* @author Young
* @param e
* @param str
* @throws InterruptedException
*/
public static void type(WebElement e, String str) throws InterruptedException
{
String[] singleCharacters = str.split("");
// Interval is 0.5 second between each type of character, this is to
// simulate real human action
for (int i = 0; i < singleCharacters.length; i++)
{
if (singleCharacters[i] != "")
{
e.sendKeys(singleCharacters[i]);
Thread.sleep(500);
}
}
Thread.sleep(1000);

}

}

原文地址:https://www.cnblogs.com/tobecrazy/p/4984551.html