定向班第一期_Selenium 入门实战_20190217

参考资料

知识串讲

   <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.12.0</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class SearchKeyword {
    WebDriver driver = null;
    @BeforeMethod
    private void setup(){
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        options.setHeadless(true);

        driver = new ChromeDriver(options);
    }

    @Test
    private void test() throws Exception{
        driver.get("https://testerhome.com/");
        //search 框定位 - css
        By searchBy = By.tagName("input");

        WebElement searchWebElement = driver.findElement(searchBy);
        searchWebElement.sendKeys("selenium");

        Actions action = new Actions(driver);
        action.sendKeys(Keys.ENTER).perform();

        Thread.sleep(5000);
    }

    @AfterMethod
    private void teardown(){
        driver.quit();
    }
}

```java
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import page.HomePage;
import page.SearchResultPage;

public class SearchKeyword {
    WebDriver driver = null;
    @BeforeMethod
    private void setup(){
        ChromeOptions options = new ChromeOptions();
        driver = new ChromeDriver(options);
        driver.manage().window().maximize();
    }

    @Test
    private void test() throws Exception{
        driver.get("https://testerhome.com/");
        HomePage homePage = new HomePage(driver);

        String keyword = "selenium";
        SearchResultPage searchResultPage = homePage.gotoSearchResult(keyword);
//        assertTopics(keyword,searchResultPage);
        String actualMsg = "";
        for(WebElement topic : searchResultPage.returnTopics()) {
            actualMsg = topic.getText().trim();
            Assert.assertTrue(actualMsg.toLowerCase().contains(keyword), "期待的标题不包含" + keyword + "实际的标题是" + actualMsg);
        }
    }

//    public void assertTopics(String value,SearchResultPage searchResultPage){
//        String actualMsg = "";
//        for(WebElement topic : searchResultPage.topics){
//            actualMsg = topic.getText().trim();
//            Assert.assertTrue(actualMsg.toLowerCase().contains(value.toLowerCase()),"期待的标题不包含" + value + "实际的标题是" + actualMsg);
//        }
//    }

    @AfterMethod
    private void teardown(){
        driver.quit();
    }
}


扩展知识

chrome的$表达式

下午实战

@DataProvider
public Object[][] getData(){
Object[][] o = {
{“carol”,“12345”},
{“jack”,“54321”},
};
return o;
}

DataProvider

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import page.HomePage;
import page.SearchResultPage;

import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SearchKeywordDataProvider {
    WebDriver driver = null;
    @BeforeMethod
    private void setup(){
        ChromeOptions options = new ChromeOptions();
        driver = new ChromeDriver(options);
        driver.manage().window().maximize();
    }

    @DataProvider
    public Iterator<Object[]> getData() throws Exception {
        return readCsvFile("D:\\\\Selenium自动化\\\\searchWords.csv");
    }

    @Test(dataProvider = "getData")
    private void test(String value) throws Exception{
        driver.get("https://testerhome.com/");
        HomePage homePage = new HomePage(driver);

        SearchResultPage searchResultPage = homePage.gotoSearchResult(value);
    }

    public Iterator<Object[]> readCsvFile(String filePath) throws Exception {
        List<Object[]> dataArray = new ArrayList<Object[]>();
        Reader in = new FileReader(filePath);
        Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
        for(CSVRecord record:records){
            List<Object> rowList = new ArrayList();
            Iterator i = record.iterator();
            while(i.hasNext()){
                rowList.add(i.next());
            }
            Object[] rowArray = rowList.toArray();
            dataArray.add(rowArray);
        }
        return dataArray.iterator();
    }

    @AfterMethod
    private void teardown(){
        driver.quit();
    }
}

xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="suite" verbose="10" parallel="methods" thread-count="2">
    <test name="testName">
        <classes>
             <class name="ClickBanner" />
             <class name="SearchKeywordDataProvider" />
        </classes>
    </test>
</suite>

作业

未登录状态下
1、打开testerhome主页
2、点击社团
3、点击霍格沃兹测试学院
4、点击话题列表中的第一个
5、验证 访问被拒绝 字符串出现

##读取yaml文件

org.yaml snakeyaml 1.23
关闭