栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

selenium批判

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

selenium批判

如果您正在使用Selenium IDE生成代码,则只需获取selenium将执行的每个动作的列表。对我来说,Selenium
IDE是启动或进行快速的“尝试一下”测试的好方法。但是,当您考虑可维护性和可读性更高的代码时,必须编写自己的代码。

实现良好的硒代码的一种好方法是使用页面对象模式,使代码代表您的导航流程。这是我在Coding
Dojo Floripa(来自巴西)中看到的一个很好的例子:

public class GoogleTest {    private Selenium selenium;    @Before    public void setUp() throws Exception { selenium = new DefaultSelenium("localhost", 4444, "*firefox",      "http://www.google.com/webhp?hl=en"); selenium.start();    }    @Test    public void codingDojoShouldBeInFirstPageOfResults() { GoogleHomePage home = new GoogleHomePage(selenium); GoogleSearchResults searchResults = home.searchFor("coding dojo"); String firstEntry = searchResults.getResult(0); assertEquals("Coding Dojo Wiki: FrontPage", firstEntry);    }    @After    public void tearDown() throws Exception { selenium.stop();    }}public class GoogleHomePage {    private final Selenium selenium;    public GoogleHomePage(Selenium selenium) { this.selenium = selenium; this.selenium.open("http://www.google.com/webhp?hl=en"); if (!"Google".equals(selenium.getTitle())) {         throw new IllegalStateException("Not the Google Home Page"); }    }    public GoogleSearchResults searchFor(String string) { selenium.type("q", string); selenium.click("btnG"); selenium.waitForPageToLoad("5000"); return new GoogleSearchResults(string, selenium);    }}public class GoogleSearchResults {    private final Selenium selenium;    public GoogleSearchResults(String string, Selenium selenium) { this.selenium = selenium; if (!(string + " - Google Search").equals(selenium.getTitle())) {         throw new IllegalStateException(   "This is not the Google Results Page"); }    }    public String getResult(int i) { String nameXPath = "xpath=id('res')/div[1]/div[" + (i + 1) + "]/h2/a"; return selenium.getText(nameXPath);    }}

希望有帮助



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393333.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号