zoukankan      html  css  js  c++  java
  • [原创]Juint4 + WebDriver 搭建自动化测试框架

    本例中用百度的搜索为例,将百度首页定义成一个待测试类 HomePage

    public class HomePage {
    private WebDriver driver;
    
    @FindBy(how = How.NAME, using = "wd")
    public static WebElement serchInputbox;
    
    @FindBy(how = How.ID, using = "su")
    public static WebElement serchBtn;
    
    @FindBy(how = How.ID, using = "container")
    public static WebElement serchResult;
    
    public HomePage(WebDriver driver) {
        this.driver = driver;
        ElementLocatorFactory finder = new AjaxElementLocatorFactory(driver,
                120);
        PageFactory.initElements(finder, this);
    
    }
    
    public void enterSerchTxt(String serchTxt) {
        serchInputbox.clear();
        serchInputbox.sendKeys(serchTxt);
    }
    
    public void clickSerchButton() {
        serchBtn.click();
    }
    
    public void checkResult() {
        assertEquals(serchResult.isDisplayed(), true);
    }
    } 

    上面的构造函数中用到了 PageFactory 这个三方类,另外定义了一些待测方法(测试用例中的小步骤)
    下面是对应于 HomePage 的测试类 homepageTest ,您可以在HomePage上右击新建 junit file ,选择 BeforeClass, Setup ...需要注意的是命名必须是以 Test 结尾。

    public class homepageTest {
    protected static WebDriver driver;
    
    @BeforeClass
    public static void beforeClass() throws Exception {
        driver = new InternetExplorerDriver();
    }
    
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        driver.quit();
    }
    
    @Before
    public void setUp() throws Exception {
        driver.get("http://www.baidu.com");
    }
    
    @After
    public void tearDown() throws Exception {
    }
    
    @Test
    public void testHomepage() {
        HomePage homepage = new HomePage(driver);
        homepage.enterSerchTxt("selenium");
        homepage.clickSerchButton();
        // maybe the net will delay, so wait for while
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        homepage.checkResult();
    }
    }

    @Test 里面便是测试用例,可以有多个 @Test。
    现在就可以编译下,run as --> junit test

    本文采用的 iedriver ,机器是64位的,会默认启动你的64位 ie(ie8分64和32位),如果您需要启32位 ie,则需要用32位的 jar 启动 selenium sever。

  • 相关阅读:
    网络编程
    mysql
    python 基础
    vim 操作
    linux 基本命令
    基本库使用(urllib,requests)
    震撼功能:逐浪CMS全面支持PWA移动生成意指未来
    硬件能力与智能AI-Zoomla!逐浪CMS2 x3.9.2正式发布
    从送外卖到建站售主机还有共享自行车说起-2017年8月江西IDC排行榜与发展报告
    HTTP协议知多少-关于http1.x、http2、SPDY的相关知识
  • 原文地址:https://www.cnblogs.com/GGHHLL/p/3124097.html
Copyright © 2011-2022 走看看