Test of St Experiment two-------Seleniumide and Visual Web page

Source: Internet
Author: User
Tags xpath

1. Install seleniumide and use it for recording and exporting scripts

1) seleniumide is Firefox plug-in, so you have to install Firefox first;

2) Open Firefox, select "Developer", select "Get More Tools", click on the "Add-ons title" in the webpage or login directly

https://addons.mozilla.org/zh-CN/firefox/collections/mozilla/webdeveloper/

3) Search Seleniumide


Add to Firefox

4) After the installation is complete, there is an icon in the upper right corner, click to open it


5) Visit the test site, use http://www.ncfxy.com here, enter the user name and password




6) Click Submit

Select the mailbox that appears in the new page, right-click Asserttext ...


7) Export Script

2. Perform simulation tests in eclipse:

1) Open Eclipse, create a new normal Java project, drag and drop the script (Java file) generated above to modify the package name.

2) Download the required library for selenium from the official website

http://www.seleniumhq.org/download/

3) Import all the jars extracted from this compact package into the project

4) Since the data used by the author is obtained from the CSV file, it is necessary to import a CSV parsing package, using Javacsv. Jar


5) Modify the script to test, note the path of the CSV file to write your own file

Package com.example.tests; Import Java.io.ioexception;import java.nio.charset.charset;import java.util.arrays;import java.util.Collection; Import Java.util.regex.pattern;import Java.util.concurrent.TimeUnit; Import Org.junit.*;import Org.junit.runner.runwith;import Org.junit.runners.parameterized;import Org.junit.runners.Parameterized.Parameters; Import static Org.junit.assert.*;import static org.hamcrest.corematchers.*; Import Org.openqa.selenium.*;import Org.openqa.selenium.firefox.firefoxdriver;import Org.openqa.selenium.support.ui.Select; Import Com.csvreader.CsvReader;  @RunWith (parameterized.class) public class Seleniumidetest {private Webdriver driver;  Private String BaseUrl;  Private Boolean Acceptnextalert = true;   Private StringBuffer verificationerrors = new StringBuffer ();    @Before public void SetUp () throws Exception {driver = new firefoxdriver ();    BASEURL = "http://www.ncfxy.com";  Driver.manage (). Timeouts (). implicitlywait (2, timeunit.seconds); } @Test Public void Testseleniumide () throws Exception {Driver.get (baseUrl + "/");    Driver.findelement (by.id ("name")). Clear ();    Driver.findelement (by.id ("name")). SendKeys (name);    Driver.findelement (By.id ("pwd")). Clear ();    Driver.findelement (By.id ("pwd")). SendKeys (Name.substring (4));    Driver.findelement (By.id ("submit")). Click ();    Asserttrue (Email.equals (Driver.findelement (By.xpath ("//tbody[@id = ' table-main ']/tr/td[2]"). GetText ()));  Assertequals (email, driver.findelement (By.xpath ("//tbody[@id = ' table-main ']/tr/td[2]"). GetText ()); } @Parameters public static collection<object[]> GetData () throws ioexception{//using parameterized tests.      Create an Object array for each read-out row from the CSV file object[][] obj = new object[109][];      Csvreader r= New Csvreader ("e:\\ courseware \ \ \ Software Test Courseware \\lab\\2\\info.csv", ', ', Charset.forname ("GBK"));           int count = 0;          while (R.readrecord ()) {Obj[count] = new Object[]{r.get (0), R.get (1)};      count++;  } return Arrays.aslist (obj);  } String name; STring Email;        Public Seleniumidetest (string name, string email) {this.name = name;  This.email = email;    } @After public void TearDown () throws Exception {driver.quit ();    String verificationerrorstring = verificationerrors.tostring (); if (! "".    Equals (verificationerrorstring)) {fail (verificationerrorstring);      }} Private Boolean Iselementpresent (by) {try {driver.findelement (by);    return true;    } catch (Nosuchelementexception e) {return false;      }} private Boolean isalertpresent () {try {Driver.switchto (). alert ();    return true;    } catch (Noalertpresentexception e) {return false;      }} private String Closealertandgetitstext () {try {alert alert = Driver.switchto (). alert ();      String Alerttext = Alert.gettext ();      if (Acceptnextalert) {alert.accept ();      } else {Alert.dismiss ();    } return alerttext;    } finally {Acceptnextalert = true; }  }}


6) test Run


Test of St Experiment two-------Seleniumide and Visual Web page

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.