Software Testing (v)

Source: Internet
Author: User

Selenium developing Java Webdriver test programs

First, install the selenium plug-in

In the Firefox browser, choose Menu-Developer-to get more tools, search for selenium, after the installation is complete, the icon in the browser indicates that the installation was successful.

  

Ii. recording and exporting scripts using Seleniumide

1. Recording Scripts

Click on the button shown to launch Seleniumide, click on the red button shown to start the recording script

    

Enter the URL in Firefox, enter www.ncfxy.com here, enter the correct username and password, click Login, right click on the location of the mailbox to select Asserttext (need to install Firepath, method with the installation seleniumide) (experimental requirements).

Here, complete the recording of the script.

You can choose, speed, execute test Cases

    

Results of execution:

    

2. Export Scripts

Select in Seleniumide, file--export test suite---java/junit4/webdriver export script.

Third, write Webdriver program, test CSV data

Create a new project in Eclipse ...

The project requires the introduction of the corresponding selenium jar packages and webdriver drivers.

The code is as follows:

  

 Packagetest;Import Staticorg.junit.Assert.assertEquals;ImportJava.io.BufferedReader;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;Importjava.io.IOException;Importjava.util.Arrays;Importjava.util.Collection;ImportJava.util.concurrent.TimeUnit;ImportOrg.junit.After;ImportOrg.junit.Before;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.junit.runners.Parameterized;Importorg.junit.runners.Parameterized.Parameters;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.firefox.FirefoxDriver; the @RunWith (parameterized.class) Public classTest {PrivateString name; PrivateString Email; PrivateWebdriver Driver; PrivateString BaseUrl;  PublicTest (String name,string email) { This. Name =name;  This. email =email; } @Before Public voidSetUp ()throwsException {driver=NewFirefoxdriver (); BASEURL= "http://www.ncfxy.com/"; Driver.manage (). Timeouts (). Implicitlywait (30, Timeunit.seconds);            Driver.get (BASEURL); } @Parameters Public StaticCollection<object[]> GetData ()throwsioexception{File inFile=NewFile ("C://users/1dell/desktop/info.csv"); object[][] obj=Newobject[109][]; String in=""; Try{@SuppressWarnings ("Resource") BufferedReader Reader=NewBufferedReader (NewFileReader (inFile)); inti = 0;  while(in = Reader.readline ())! =NULL) {Obj[i]=NewObject[]{in.split (",") [0], In.split (",") [1]}; I++; }                              } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnarrays.aslist (obj); } @After Public voidTearDown ()throwsException {driver.close (); } @Test Public voidTest () {webelement element= Driver.findelement (by.id ("name")); Element.sendkeys ( This. Name); Webelement element1= Driver.findelement (By.id ("pwd")); Element1.sendkeys ( This. name.substring (4)); Webelement Element3= Driver.findelement (By.id ("Submit"));                Element3.click (); Assertequals ( This. Email, driver.findelement (By.xpath (".//*[@id = ' table-main ']/tr[1]/td[2]") . GetText ()); }}

Some of the test results are as follows:

PS: The test result is slow, do not know what reason.

  

  

Software Testing (v)

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.