Selenium-java Web Automation Testing Tools

Source: Internet
Author: User
Tags xpath java web seleniumhq

The origin of this article, the two days to tidy up their regular use and very skilled projects, today suddenly think of missing one, fill up, but think of no corresponding blog, then write a simple

Some of the techniques I often use and are relatively skilled are as follows (I don't know if it counts as the spicy chicken in the eyes of the Gods):

Traditional Web project testing, most of them rely on the test Department of Small Partners manual operation, time-consuming and easy to side-leakage ' (*∩_∩*) wrong is the leak test,

Selenium, however, provides a great convenience for the testing of the project, but not all projects are suitable,

What is appropriate: The company's own products, and the need for frequent regression testing, such as OA-like business system products

Not suitable: such as outsourcing business, fast delivery after the no matter, only need to test one or two times

Today I wrote a simple example in Java, first look at the execution effect (click on the image to see the HD version):

Is the effect satisfactory? Please continue to look down, not satisfied please close the window thank you

1. New MAVEN project, introducing Selenium-java

<!--Https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -        <Dependency>            <groupId>Org.seleniumhq.selenium</groupId>            <Artifactid>Selenium-java</Artifactid>            <version>3.8.1</version>        </Dependency>

2. Write the code (because the automated test is very fast, I have a little pause after each step, the comments are clear)

1  PackageCom.xiao.selenium;2 3 Importjava.util.List;4 ImportJava.util.Random;5 ImportJava.util.concurrent.TimeUnit;6 7 Importorg.openqa.selenium.By;8 ImportOrg.openqa.selenium.JavascriptExecutor;9 ImportOrg.openqa.selenium.WebDriver;Ten Importorg.openqa.selenium.WebElement; One ImportOrg.openqa.selenium.firefox.FirefoxDriver; A  -  Public classAutoTest { -      Public Static voidMain (string[] args)throwsinterruptedexception { theThread.Sleep (3000); -         //system.setproperty ("Webdriver.chrome.driver", "C://Chromedriver.exe "); -         //webdriver webdriver = new Chromedriver (); -System.setproperty ("Webdriver.gecko.driver", "C://geckodriver.exe"); +Webdriver Webdriver =Newfirefoxdriver (); - webdriver.manage (). window (). Maximize (); + webdriver.manage (). Deleteallcookies (); A         //syncing with the browser is important and must wait for the browser to load atWebdriver.manage (). Timeouts (). Implicitlywait (10, timeunit.seconds); -          -         //Open Destination Address -Webdriver.get ("http://192.168.0.32:88"); -         //Enter your account password and log in to the system -Thread.Sleep (1000); inWebdriver.findelement (By.xpath ("/html/body/div/div/form/div[1]/div/div/input")). SendKeys ("admin")); -Webdriver.findelement (By.xpath ("/html/body/div/div/form/div[2]/div/div/input")). SendKeys ("123456")); toWebdriver.findelement (by.cssselector ("HTML body div#app div.loginpage Form.el-form.frombox Button.el-button.loginbtn.el-button--primary ") . Click (); +          -         //Select System theThread.Sleep (1000); *Webdriver.findelement (By.xpath ("/html/body/div/div/div/div[1]/p") . Click (); $         Panax Notoginseng         //Expand the basic Information Management menu -Thread.Sleep (1000); theWebdriver.findelement (By.xpath ("/html/body/div/div/div/div[1]/div[3]/ul/div[1]/li/div/span") . Click (); +         //Click the section Management menu AThread.Sleep (1000); theWebdriver.findelement (By.cssselector (". is-opened > Ul:nth-child (2) > Li:nth-child (1)") . Click (); +          -         //Skip to page 2nd $Thread.Sleep (1000); $Webdriver.findelement (By.xpath ("/html/body/div/div/div/div[2]/div[2]/div/div[2]/div[2]/div/div[2]/ul/li[2]") . Click (); -          -         //Click the New button theWebdriver.findelement (By.xpath ("/html/body/div/div/div/div[2]/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/ button) . Click (); -         Wuyi         //randomly generate text box content based on rules the         intRandom =NewRandom (). Nextint (200000000); -Thread.Sleep (1000); WuWebdriver.findelement (By.xpath ("/html/body/div[1]/div/div/div[2]/div[2]/div/div[3]/div/div[2]/form/div[1]/div[ 1]/div/div/input ") . SendKeys (string.valueof (random)); -Thread.Sleep (1000); AboutWebdriver.findelement (By.xpath ("/html/body/div[1]/div/div/div[2]/div[2]/div/div[3]/div/div[2]/form/div[1]/div[ 2]/div/div[1]/input ")). SendKeys (" Automated test-"+random); $          -         //Expand the drop-down box -Webdriver.findelement (By.xpath ("/html/body/div[1]/div/div/div[2]/div[2]/div/div[3]/div/div[2]/form/div[2]/div[ 1]/div/div/div[1]/span/span/i ") . Click (); -Thread.Sleep (1000); A         //Get drop-down box size +list<webelement> Select1 = webdriver.findelements (By.cssselector ("Div.el-select-dropdown:nth-child (4) > Div:nth-child (1) > Div:nth-child (1) > UL li ")); the         //randomly select an item -         intSelectItem1 =NewRandom (). Nextint (Select1.size ()) +1; $Webdriver.findelement (By.xpath ("/html/body/div[3]/div[1]/div[1]/ul/li[" +selectitem1+ "]") . Click (); the          the         //pause for a moment and then save theThread.Sleep (1000); theWebdriver.findelement (By.xpath ("/html/body/div[1]/div/div/div[2]/div[2]/div/div[3]/div/div[3]/div/button[1]") . Click (); -          in         //Jump to my blog theThread.Sleep (3000); theWebdriver.get ("Https://www.cnblogs.com/xiaochangwei"); About          theWebdriver.findelements (By.classname ("Posttitle")). ForEach (X- { the System.out.println (X.gettext ()); the         }); +          -Thread.Sleep (1000); the         //open an article titled Building a Mirror from Dockerfile and publishing a Web projectBayiWebdriver.findelement (By.partiallinktext ("Build image from Dockerfile and publish Web project") . Click (); the          theThread.Sleep (1000); -         //move to the bottom -         //( (Javascriptexecutor) webdriver). Executescript ("Window.scrollto (0, Document.body.scrollHeight)"); the         //moves to the specified coordinates (moving relative to the current coordinate) the((Javascriptexecutor) webdriver). Executescript ("Window.scrollby (0, 700)");  theThread.Sleep (1000); the         //move to the window absolute position coordinates, move to the ordinate 1600 pixel position as follows -((Javascriptexecutor) webdriver). Executescript ("Window.scrollto (0, 1600)");  theThread.Sleep (1000); the         //moves to the specified element, and aligns the reference at the bottom of the element and the bottom of the windowhttps://www.cnblogs.com/testway/p/6693140.html the((Javascriptexecutor) webdriver). Executescript ("Arguments[0].scrollintoview (false);", Webdriver.findelement ( By.xpath ("/html/body/div[2]/div[2]/div[1]/div/div[1]/div/div/div[2]/div[4]/div[3]/div[1]/a[5]/img")));94         //pause after five seconds to close theThread.Sleep (2000); the webdriver.quit (); the     }98}

which

16-line Google Browser driver: https://chromedriver.storage.googleapis.com/index.html

18-line Firefox driver: https://github.com/mozilla/geckodriver/releases

And then run, you can do whatever you like.

Selenium-java Web Automation Testing Tools

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.