ST: (LAB2) Selenium experiment

Source: Internet
Author: User

First, the contents of the experiment

    1. Installing the Seleniumide Plugin
    2. Learn to use Seleniumide to record scripts and export scripts
    3. Access http://www.ncfxy.com Use the number login system (account name is the school number, the password is the number 6), after entering the system can see the user's mailbox.
    4. Write the Selenium Java webdriver program to test the correct relationship between the number and the mailbox in the Info.csv table.
    5. Submit the test code to GitHub.

Ii. purpose of the experiment

    1. Understand and master the use of seleniumide , and use seleniumide for automated testing of Web pages.
    2. Build a test project with maven from the start.
    3. Learn to write selenium Java webdriver programs, and then test the data.

Third, the experimental process

    1. Installing the Seleniumide Plugin

First download Firefox, and download and install the Seleniumide plugin in the web Developer in the toolbar.

Once the plugin is installed, the browser's toolbar will have one more tool, as shown in:

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

1) Recording script:

A) Click the Selenium IDE in the Firefox toolbar and click the Red button on the right to start the recording script. (By default, the script starts recording after you open the Selenium IDE, so you don't usually have to click the red button at the beginning)

b) Enter http://www.ncfxy.com in the browser to access the URL.

c) Enter the correct number and password information on the page and click Submit.

d) After clicking, the red button on the right stops recording. The process is as follows:

2) Export script:

A) in the Seleniumide box, click File->export Test Case as....-> java/junit4/webdriver to export the. java file. As shown in the following:

    1. Write the Selenium Java webdriver program to test the correct relationship between the number and the mailbox in the Info.csv table.

1) Create a new Maven Project in Eclipse, as shown in:

2) Create the test class webtest under SRC, and open the exported test script with Notepad, paste it into the test class, and modify the package name and class name accordingly.

3) The following types of. jar files are introduced under the test project:

4) Make the appropriate changes in the exported test script:

Incoming two parameters represent ID and email, respectively. Then @test the next loop read each line in the Info.csv file, read it out to the ID and email, and then test it individually for each ID in the loop, and when the password is entered correctly, test the info.csv in the table with the correct relationship between the number and the mailbox. The test code is as follows:

 Packagecom.example.tests;ImportJava.nio.charset.Charset;ImportJava.util.regex.Pattern;ImportJava.util.concurrent.TimeUnit;Importorg.junit.*; Import Staticorg.junit.assert.*;Import Staticorg.hamcrest.corematchers.*; Importorg.openqa.selenium.*;ImportOrg.openqa.selenium.firefox.FirefoxDriver;ImportOrg.openqa.selenium.support.ui.Select;ImportCom.csvreader.CsvReader; Public classWebTest {PrivateWebdriver Driver; PrivateString BaseUrl; Private BooleanAcceptnextalert =true; PrivateStringBuffer verificationerrors =NewStringBuffer (); PrivateString ID =NULL; PrivateString pwd =NULL; PrivateString email =NULL; @Before Public voidSetUp ()throwsException {driver=NewFirefoxdriver (); BASEURL= "http://www.ncfxy.com/"; Driver.manage (). Timeouts (). Implicitlywait (30, Timeunit.seconds); } @Test Public voidTESTEXP2 ()throwsException {csvreader cr=NewCsvreader ("c:/users/administrator/desktop/software test/second on-Machine/info.csv", ', ', Charset.forname ("GBK"));  while(Cr.readrecord ()) {ID= Cr.get (0); Email= Cr.get (1); PWD= Id.substring (4, 10);        Driver.get (BASEURL); Driver.findelement (By.id ("Name") . Clear (); Driver.findelement (By.id ("Name") . SendKeys (ID); Driver.findelement (By.id ("PWD") . Clear (); Driver.findelement (By.id ("PWD") . SendKeys (PWD); NewSelect (Driver.findelement (by.id ("Gender"))). Selectbyvisibletext ("Woman")); Driver.findelement (By.id ("Submit") . Click (); String Number= Driver.findelement (By.xpath ("//tbody[@id = ' table-main ']/tr[2]/td[2]") . GetText (); String Email1= Driver.findelement (By.xpath ("//tbody[@id = ' table-main ']/tr[1]/td[2]") . GetText (); if(id.equals (number)) {assertequals (EMAIL,EMAIL1); } }} @After Public voidTearDown ()throwsException {driver.quit (); String verificationerrorstring=verificationerrors.tostring (); if(!"". Equals (verificationerrorstring))    {fail (verificationerrorstring); }  }   Private Booleaniselementpresent (by) {Try{driver.findelement (by); return true; } Catch(nosuchelementexception e) {return false; }  }   Private Booleanisalertpresent () {Try{Driver.switchto (). alert (); return true; } Catch(noalertpresentexception e) {return false; }  }   PrivateString Closealertandgetitstext () {Try{alert Alert=Driver.switchto (). alert (); String Alerttext=Alert.gettext (); if(Acceptnextalert) {alert.accept (); } Else{Alert.dismiss (); }      returnAlerttext; } finally{Acceptnextalert=true; }  }}

Iv. Results of the experiment

By using seleniumide and Maven, and by writing selenium Java webdriver programs, Web pages were successfully tested automatically.

ST: (LAB2) Selenium experiment

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.