Selenium framework all use cases open once browser method implementation (Java)

Source: Internet
Author: User
Tags log log testng

The previously written automated test framework (PageObject design idea) has always been to execute a use case, start a browser driver to open a browser, a use case after the end of the browser close. This is not only a waste of time, but it is also a waste of resources to restart and close chromedriver.exe every time. This function is realized by making a little change to the relevant methods according to the order of execution of each annotation in the testng life cycle.

First look at the Pagefactory class, this class is mainly the use of Java reflection mechanism, call the construction method of the page class to generate a new Page object, here I define a Webdriver object in this class, In the future, this driver object will be the unified driver of all the page objects generated by the Pagefactory class, theoretically implementing a scenario where there is only one driver object, the code is as follows:

Pagefactory:

 Public classPagefactory {Private StaticWebdriver Driver;  Public StaticWebdriver Getdriver () {returndriver; }     Public Static voidsetdriver (webdriver driver) {pagefactory.driver=driver; }    StaticLog log =NewLog (pagefactory.class); /*** @Description: Generate new Page *@paramKey     * @returnObject * @Author: zither*/     Public synchronized StaticObject GetPage (class<?>key) {Object obj=NULL; Try{String Pageclassname=Key.getcanonicalname (); Log.debug ("Jump to Page" +key.getsimplename ()); Class<?> Clazz =Class.forName (pageclassname); Try{Constructor<?> constructor = Clazz.getconstructor (Webdriver.class); Obj=constructor.newinstance (Getdriver ()); } Catch(instantiationexception e) {log.error (E.getmessage ());            E.printstacktrace (); } Catch(illegalaccessexception e) {log.error (E.getmessage ());            E.printstacktrace (); }        } Catch(Exception e) {}returnobj; }}

Then I created a Testsuite class, which is the TestNG class, mainly used to include some common @beforesuite, @AfterSuite and so on, and then each testng class can directly inherit this class without repeating these annotations,

Here is a special focus on what @beforesuite and @beforemethod do, and the code is as follows:

TestSuite:

 Public classTestSuite { Public StaticWebdriver Driver; /*** @Description: Create driver and give driver to Pagefactory page Factory *@returnvoid * @Author: zither*/@BeforeSuite Public voidBeforesuite () {driver=Driverfactory.getchromedriver ();    Pagefactory.setdriver (driver); } @BeforeMethod Public voidBeforemethod () {driver.get (Config.url); }    /*** @Description: Suite ends Close Browser, end driver *@returnvoid * @Author: zither*/@AfterSuite Public voidAftersuite () {driverfactory.close (driver); }}

Take a look at how the action class is written, the page object in the action business flow is generated by the pagefactory that has been obtained by the driver, it is possible to directly obtain a copy of the unique driver, But there's actually only one driver. In the operation of the browser and page, the action class is written as follows (to open the browser to the homepage, click Login, enter the login page, login, return to the homepage of the business flow as an example):

Homeac

 public  class   HOMEAC { static      HOMEPG HOMEPG;  /**   * @Description: XXXX Home Click Login to enter the login page *  @return   LOGINPG * @Author: zither  */ public  static
      LOGINPG Hometologin () {HOMEPG  = (HOMEPG) pagefactory.getpage (Homepg.
     
      class 
       return  (LOGINPG) pagefactory.getpage (Loginpg.    Class  }  

Loginac

 Public classLoginac {StaticLOGINPG LOGINPG; /*** @Description: Login page Login back to XXXX homepage *@paramAccount *@paramPassword *@returnHOMEPG * @Author: zither*/       Public Statichomepg logintohomepg (String account,string password) {LOGINPG=Homeac.hometologin ();        Loginpg.typeaccount (account);        Loginpg.typepassword (password);        Loginpg.clicksubmit (); return(HOMEPG) Pagefactory.getpage (HOMEPG.class); }}

As a result of our @test each execution of a use case, it will be because the @beformethod inside the address bar to reopen the home page link to go back to the home page, continue to start the next use case, and do not have to switch off the browser and restart the browser so much trouble ~ ~

In fact, this is only for the first entry is the home page needs to click Login and then login to the pages, because this site after landing once the URL or the homepage can be directly to the next use case operation and no more landing.

In the next article, the URL goes directly to "login page-to-page" instead of "home---login-to-home" website What to do

  

Selenium framework all use cases open once browser method implementation (Java)

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.