Selenium test Framework (Java) version Evolution one

Source: Internet
Author: User
Tags assert testng

How should selenium's automated test code be organized? such as Link: https://code.google.com/p/selenium/wiki/PageObjects here provides a kind of pageobject design idea, and in Baidu Interior gives a feeling more practical realization. Its organizational structure is thought as follows:

Page    封装页面元素,以及页面应提供的服务。        尽量隐藏页面的细节,不要暴露出来。widget  封装Page中的通用的组件。        这里的理念是所有的WebElement都是控件。        通用的页面样式,如导航栏,列表,组合查询框,可以封装成一个大控件。Data    封装测试数据。Test    封装测试驱动代码。推荐使用testng。

This article intends to re-establish a simplified test framework.

The first version builds a measurable selenium project. Contains a page base class, an inherited actual page to be tested Baidumainpage, a driver test case baidumainpagetest.

The following need to be improved: The driver used in 1,baidumainpagetest is explicitly stated in Test as Firefox. It should be configured to use any one of the driver. And there is a need to implement a base class to accomplish these things.

2, the element can be initialized and the process of opening the page is encapsulated into a navigation class. In addition, nosuchelementexception often occurs. After adding the wait, the basic solution. However, it is necessary to troubleshoot the cause and suspect that it has not been loaded in time.

Framework Code

The project uses MAVEN management:

<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> &L    T;artifactid>selenium-java</artifactid> <version>2.41.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactid>slf4j-api</artifactid > <version>1.7.5</version> </dependency> <dependency> <groupid>or G.slf4j</groupid> <artifactId>slf4j-simple</artifactId> <version>1.7.5</versi on> </dependency> <dependency> <groupId>org.testng</groupId> <ar Tifactid>testng</artifactid> <version>6.8.8</version> </dependency>
BasePage
/** * <p>the <b>page object</b> pattern represents the screens of your web app as a series of * objects. </p> * * <p>pageobjects can is thought of as facing in both directions simultaneously. * Facing towards the developer of a test, they represent the services offered * by a particular page. Facing away from the developer, they should is the only * thing that have a deep knowledge of the structure of the HTML of A page (or * part of a page) It's simplest to think of the methods in a page Object as * offering the "services" that a PA GE offers rather than exposing the details * and mechanics of the page.</p> * * @see <a href= "Http://code.google . com/p/webdriver/wiki/pageobjects ">page Objects wiki</a> * * @author Lizejun */public abstract class Page Implem    Ents searchcontext{protected Logger Logger = Loggerfactory.getlogger (GetClass ());    protected Webdriver driver = null;  Public Page (Webdriver driver) {if (null = = driver) {          throw new IllegalArgumentException ("Webdriver cannot be null.");    } this.driver = driver;    } public Webdriver Getwebdriver () {return driver;    }/** * Gets the current address * @return */public String Getcurrenturl () {return driver.getcurrenturl ();    }/** * Gets an element */public webelement findelement (by) {return driver.findelement (by);    }/** * GET compliant */public list<webelement> findelements (by) {return driver.findelements (by); }/** * for hard wait * @param seconds */public void sleep (int seconds) {try {timeunit.        Seconds.sleep (SECONDS);        } catch (Interruptedexception e) {e.printstacktrace (); }}/** * is used to wait for an element to appear, soft wait * @param by */public Boolean Wait (final by) {New Webdriverwait (dr          Iver). Until (new expectedcondition<boolean> () {public Boolean apply (Webdriver arg0) {      Return Arg0.findelement (by). Isdisplayed ();        }        });    return true;        }/** * Handles pop-up alert () * @param option */public Alert Getalert () {sleep (2);        Alert alert = Driver.switchto (). alert ();    return alert; }/** * Simulate keyboard arrow down, then enter.        That is, the analog drop-down is selected */public void Keydownandenter () {sleep (1);        actions = new actions (driver);    Actions.sendkeys (Keys.arrow_down). SendKeys (Keys.enter). Build (). Perform ();        }/** * Analog keyboard carriage return */public void Keyenter () {sleep (1);        actions = new actions (driver);    Actions.sendkeys (Keys.enter). Build (). Perform ();        /** * Closes the current page and places the focus on the new page that opens */public void Switchtonewpage () {sleep (1);        String handlenew= "";        For (String handle:driver.getWindowHandles ()) {handlenew=handle;        } driver.close ();        Sleep (1);    Driver.switchto (). window (handlenew); }}
Demopage
public class BaiduMainPage extends Page{    public BaiduMainPage(WebDriver driver) {        super(driver);    }    /**     * 输入文本input     */    @FindBy(how=How.ID,using="kw1")    WebElement inputbox;    /**     * 查询按钮     */    @FindBy(how=How.ID, using="su1")    WebElement submitbox;    /**     * 新闻链接     */    @FindBy(name="tj_news")    WebElement newsIndex;    /**     * 输入待搜素文本     * @param text     */    public void SearchText(String text) {        inputbox.sendKeys(text);        submitbox.click();    }    public void clickNewsIndex() {        newsIndex.click();    }}
TestCase with testng
public class baidumainpagetest{protected webdriver driver = null;    protected Baidumainpage page = null;    @BeforeClass public void init () {driver = new firefoxdriver ();        } @BeforeMethod public void Initpage () {String path = "http://www.baidu.com/";        Driver.get (path);        page = new Baidumainpage (driver);    Pagefactory.initelements (Driver, page);        } @Test public void Testsearch () {String text= "Apple"; Page.        SearchText (text);        Assert.asserttrue (Driver.gettitle (). Contains (text));    System.out.println (Page.getcurrenturl ());        } @Test public void Testnews () {page.clicknewsindex ();        Page.sleep (2);        System.out.println (Page.getcurrenturl ());    Assert.asserttrue (Page.getcurrenturl (). Contains ("news.baidu.com"));    } @AfterClass public void Quit () {driver.quit (); }}

Selenium test Framework (Java) version Evolution one

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.