Selenium +java multiple Common driver issues

Source: Internet
Author: User
Tags xpath

Problem point: Too long did not write selenium code, incredibly put driver common problem forget, namely: Each write a test class, the execution process will create a new window, this should say is very unprofessional.
Probably think of a method, although it does not seem professional, but feel able to use it is very happy.
Steps to resolve:
1 Create a method to get driver Getdriver ()
2 Create a member variable and assign Getdriver () to a member variable
3 Other business classes when using driver, you can get the same driver through the parent class. Driver Way

    public class login{private static final String username= "[email protected]";    private static final String password= "123456";    public static final Webdriver driver=getdriver (); Public Login () {}//Get driver public static Webdriver Getdriver () {System.setproperty ("Webdriver.chrome.driver", "othe r/. /.. /..       /chromedriver.exe ");       Webdriver drivers=new chromedriver ();    return drivers;         } public static void Loginuser () {Webdriver driver=login.driver;         Driver.get ("Http://testAdress.com/#/login");         Driver.navigate (). Refresh ();         Sleep (1); Driver.findelement (By.xpath ("/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div[1]/form/div[1]/div/div/input")         ). SendKeys (UserName); Driver.findelement (By.xpath ("/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div[1]/form/div[2]/div/div[1]/         Input ")). SendKeys (password); Driver.findelement (By.xpath ("/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div[1]/form/div[4]/button")). Click (); }}

Hint: If it is useful, please do not copy, because only a part of the code, not the complete class paste out!

Selenium +java multiple Common driver issues

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.