zoukankan      html  css  js  c++  java
  • 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);

    }

    }

  • 相关阅读:
    java面试题之简单介绍一下集合框架
    java面试题之hashcode相等两个类一定相等吗?equals呢?相反呢?
    java面试题之什么是ThreadLocal?底层如何实现的?
    java面试题之stop()和suspend()方法为何不不推荐使⽤?
    设计模式—单例模式
    Java并发—同步容器和并发容器
    Java并发—并发工具类
    Java并发—原子类,java.util.concurrent.atomic包(转载)
    Java并发—java.util.concurrent.locks包
    Java并发—java.util.concurrent并发包概括(转载)
  • 原文地址:https://www.cnblogs.com/tobecrazy/p/4984551.html
Copyright © 2011-2022 走看看