Software testing LAB2 ———— automated testing with selenium

Source: Internet
Author: User
Tags file info xpath

This experiment was conducted using selenium for automated testing

The following steps are mainly divided into:

1. Install Seleniumide plug-in

2. Learn to use Seleniumide to record scripts and export scripts

3, Access http://www.ncfxy.com Use the number of login system (account name is the school number, the password for the school number after 6), enter the system can see the user's mailbox.

4, write Selenium Java webdriver program, test info.csv form the number and the correspondence between the mailbox is correct.

5. Submit the test code to GitHub.

First, install the Seleniumide plug-in

Seleniumide is one of Firefox's add-ons, first we want to download this plugin

We click on the Firefox menu and select Add-ons

Search the Selenium IDE in the search box in the upper right corner

We install using the most user sort, the first one is the plugin we need to install

After we install the components and reboot, we can use this component normally.

Second, learn to use Seleniumide to record scripts and export scripts

We first click on the developer in the menu

Open the Selenium IDE

Now let's introduce the Selenium IDE's operating interface

The left is a list of test cases for selecting the script to test

The Base URL is the destination URL for the script we're recording, and all our actions on that site are recorded.

Fast-low is used to adjust the speed of the test, sometimes the test speed is too fast to cause the test results of the anomaly

The two buttons in the middle are the same example of testing multiple samples and test orders, respectively

The red button on the right indicates the status of the recording, while the middle is a light red

The main part is the recorded script content, mainly records the user's related actions on the webpage

We take Baidu as an example to record the script

We first in the base URL to enter the Baidu URL, and then open the Baidu homepage, and search "123"

The recording results are as follows

We run the script we just recorded

Single use case test success

We then create a new script file, this time trying to search for "456" through Baidu

Test two scripts at a time

We can save and export the recorded script, taking the first script as an example, and we export it as a java/junit/webdriver backed

The code reads as follows:

 Packagecom.example.tests;ImportCom.thoughtworks.selenium.Selenium;ImportOrg.openqa.selenium.firefox.FirefoxDriver;ImportOrg.openqa.selenium.WebDriver;ImportCom.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;ImportOrg.junit.After;ImportOrg.junit.Before;Importorg.junit.Test;Import Staticorg.junit.assert.*;ImportJava.util.regex.Pattern;Import StaticOrg.apache.commons.lang3.StringUtils.join; Public classTestCase {PrivateSelenium Selenium; @Before Public voidSetUp ()throwsException {webdriver driver=NewFirefoxdriver (); String BASEURL= "https://www.baidu.com/"; Selenium=Newwebdriverbackedselenium (Driver, BASEURL); } @Test Public voidTesttestcase ()throwsException {selenium.open ("/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=monline_3_dg&wd=123&rsv_pq=bbc493ca0001f3db &rsv_t=cf61DU8FFmN%2FwF8kfsBVvVjHVj4%2F98NLPfJbcMPOhuJh%2BZ8Wy%2BtPsx2zih2QNvtABjhg&rsv_enter=0& rsv_sug3=4&rsv_sug1=3&rsv_sug7=100&inputt=1906&rsv_sug4=2804 "); Selenium.click ("Css=img[alt=\" to Baidu homepage \ "]"); Selenium.waitforpagetoload ("30000"); Selenium.click ("Id=kw"); Selenium.type ("Id=kw", "123"); Selenium.click ("Id=su"); } @After Public voidTearDown ()throwsException {selenium.stop (); }}

Third, access to the http://www.ncfxy.com use the number of login system (account name is the school number, the password for the school number after 6), enter the system can see the user's mailbox

We first visit http://www.ncfxy.com

On the homepage We can view the mailbox information by entering the user name and password

Our fourth step is to test whether the user name and the mailbox in the local file info.csv are exactly the same as the information on the site.

Iv. write the Selenium Java webdriver program, test the Info.csv table of the number and the correspondence between the mailbox is correct

Each line in the CSV file includes only the number and the mailbox, we write the Webdriver program has two main functions, part is to parse the local info.csv file to obtain the information, and the other part is to use the information resolved to verify the correctness of the mailbox

The CSV file is delimited by commas, and a row represents a single piece of data

For the resolution of the CSV file, we now import the jar package to complete the parsing work, our team uses javacsv, the jar package is only 14k size, but enough full CSV file parsing

First we add header ID, email in the original CSV file

The specific CSV file parsing process is as follows:

// Read csv file        New Csvreader ("F://info.csv", ', ', Charset.forname ("Utf-8"));        R.readheaders ();                  while (R.readrecord ()) {            = r.get ("id");             = name.substring (4);             = R.get ("email");                    }        R.close ();

The second part of the content, we first want to see the target URL page elements

On the first page we mainly need the following three information

There are ID elements we can get directly through the By.id () method

After jumping the page, we need to get the second column of the first row in the table, which is the mailbox information

Because the second column of the first row in the table does not have an ID value and a name value, we can do this in two ways

Webelement Text = driver.findelement (By.xpath (".//*[@id = ' table-main ']/tr[1]/td[2]"= driver.findelement ( By.cssselector ("#table-main tr:first-child td:last-child"));

In the course of the experiment, we used four browsers to complete the experiment content, the complete code is as follows

Import Staticorg.junit.assert.*;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.nio.charset.Charset;ImportOrg.junit.After;ImportOrg.junit.Before;Importorg.junit.Test;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.firefox.FirefoxDriver;ImportOrg.openqa.selenium.chrome.*;ImportOrg.openqa.selenium.edge.EdgeDriver;ImportOrg.openqa.selenium.firefox.FirefoxDriver;ImportOrg.openqa.selenium.ie.InternetExplorerDriver;Importorg.openqa.selenium.remote.DesiredCapabilities;ImportCom.csvreader.CsvReader; Public classtest1 {@Before Public voidSetUp ()throwsException {} @After Public voidTearDown ()throwsException {} @Test Public voidTest ()throwsException {webdriver driver=NULL; //when testing through some browsers, page load is slower than program execution, we need to let the program wait for the page to load, for example, by creating a new thread to suspend the program for a period of timeThread thread =NewThread (); //Read CSV fileCsvreader r =NewCsvreader ("F://info.csv", ', ', Charset.forname ("Utf-8"));        R.readheaders (); /*We tried the selenium function on four browsers, and in order to enhance the portability of the code, we put all the browser drivers in the engineering directory, by setting the Webdriver. Xxx.bin and Webdriver.XXXX.driver (Driver folder in the project directory) to launch the browser*/        //For Firefox//system.setproperty ("Webdriver.firefox.bin", "E:/firefox/firefox.exe");Driver =NewFirefoxdriver (); //For Chrome        /*String ch_driver = new file (new file (".").        Getcanonicalpath () + "\ \" + "Driver/chromedriver.exe"). Getcanonicalpath ();        SYSTEM.OUT.PRINTLN (driver);        System.setproperty ("Webdriver.chrome.driver", ch_driver); System.setproperty ("Webdriver.chrome.bin", "c:\\users\\" to \\AppData\\Local\\Google\\Chrome\\Application\\        Chrome.exe ");        Driver = new Chromedriver (); */        //Open IE        /*String iedriver = new file (new file (".").        Getcanonicalpath () + "\ \" + "Driver/iedriverserver.exe"). Getcanonicalpath ();        System.out.println (Iedriver);        System.setproperty ("Webdriver.ie.driver", iedriver);        System.setproperty ("Webdriver.chrome.bin", "C:\\Program files\\internet Explorer\\iexplore.exe");           Desiredcapabilities iecapabilities = Desiredcapabilities.internetexplorer ();           Iecapabilities.setcapability (Internetexplorerdriver.introduce_flakiness_by_ignoring_security_domains, true);        Driver = new Internetexplorerdriver (iecapabilities); */        //Open Edge        /*String edgedriver = new file (new file (".").        Getcanonicalpath () + "\ \" + "Driver/microsoftwebdriver.exe"). Getcanonicalpath ();        System.out.println (Iedriver);        System.setproperty ("Webdriver.edge.driver", edgedriver);                System.setproperty ("Webdriver.edge.bin", "C:\\Program files\\internet Explorer\\iexplore.exe");        Driver = new Edgedriver (); */         while(R.readrecord ()) {String name= R.get ("id"); String Password= Name.substring (4); String Email= R.get ("email"); Driver.get ("http://www.ncfxy.com/"); //window Maximizationdriver.manage (). window (). Maximize (); Webelement Txtbox1= Driver.findelement (by.id ("name"));            Txtbox1.sendkeys (name); Webelement Txtbox2= Driver.findelement (By.id ("pwd"));            Txtbox2.sendkeys (password); Webelement btn= Driver.findelement (By.id ("Submit"));            Btn.click (); Thread.Sleep (1000); //the following two sentences run the same result//webelement Text = driver.findelement (By.xpath (".//*[@id = ' table-main ']/tr[1]/td[2] "));Webelement Text = driver.findelement (By.cssselector ("#table-main tr:first-child td:last-child")); String Email2=Text.gettext ();        Assertequals (email, email2);    } r.close (); }}

In Firefox, for example, the results are as follows:

All the contents of this experiment have been completed. Some problems encountered in the experiment and the solutions

(1), the browser will not be able to access the target Web page after timeout, the program run aborted:

Possible reasons: 1, the version of Firefox and Selenium jar package version does not match, we recommend the use of Firefox45.0.1 and selenium2.53.0

2, there are a variety of Java IDE installed on this machine, some IDE will change the settings when installed, if one IDE does not work, please try Other

(2), the program running process prompts no Such Element

Possible reasons: page load is slower than program execution speed, should let the program pause waiting whenever a page is opened

Software testing LAB2 ———— automated testing with selenium

Related Article

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.