Selenium automated Web-based testing

Source: Internet
Author: User
Tags seleniumhq

1. Download necessary tools and jar packages

1. Download and install Firefox

(1) download selenium ide

Http://release.seleniumhq.org/selenium-ide/1.9.0/selenium-ide-1.9.0.xpi

(2) download test suite batch Converter

Https://addons.mozilla.org/en-US/firefox/addon/test-suite-batch-converter-sel/

(3) install the Firefox plug-in

Drag the attachment to be installed to Firefox to install it.

2. Download The selenium-server-standalone-2.28.0.jar from the address below, which is used to publish commands to Firefox.

Http://seleniumhq.org/download/

Ii. Record the script and convert it into a Java Script

1. Recording script.

Open the Firefox browser and enable selenium ide such

By default, recording is enabled. Selenium records any operations on Firefox. For example, enter "www.baidu.com" in Firefox and search

"Selenium", such

Selenium ide Interface

Click the button selected in the red box to stop recording the script.

Click the files in selenium IDE and select export test case as --- Java JUnit 4/Web driver. Here I select the Java unit test script.

Save the name. The Code is as follows:

Package COM. example. tests; import Java. util. regEx. pattern; import Java. util. concurrent. timeunit; import Org. JUnit. *; 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; public class test {private WebDriver driver; private string baseurl; private stringbuffer ve Riicationerrors = new stringbuffer (); @ beforepublic void setup () throws exception {driver = new firefoxdriver (); baseurl = "http://www.baidu.com/"; driver. manage (). timeouts (). implicitlywait (30, timeunit. seconds) ;}@ testpublic void test () throws exception {driver. get (baseurl + "/"); driver. findelement (. ID ("kW ")). clear (); driver. findelement (. ID ("kW ")). sendkeys ("Selenium"); driver. findelement (. ID ("Su ")). Click (); driver. findelement (. linktext ("Selenium _ Baidu Encyclopedia ")). click () ;}@ afterpublic 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 ;}}}

Create a new Java project, place the code in a new class, and then import the downloaded selenium-server-standalone-2.28.0.ja into the project. The final project structure is as follows:

In Testa. Java, right click --- run as and select JUnit to run.

The test results are as follows:

After the script runs, a Firefox window is automatically opened and then executed in the order of the previous operations.

There are two points to note.

(1) When importing the recorded script to a newly created Project, you need to import the Java JUnit package.

(2) unable to connect to host 127.0.0.1 on port7055 after
45000 MS, the specific error code is as follows

org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:*** LOG addons.manager: Application has been upgraded*** LOG addons.xpi: startup*** LOG addons.xpi: Skipping unavailable install location app-system-local*** LOG addons.xpi: Skipping unavailable install location app-system-share*** LOG addons.xpi: Ignoring file entry whose name is not a valid add-on ID: C:\Users\god\AppData\Local\Temp\anonymous13269webdriver-profile\extensions\webdriver-staging*** LOG addons.xpi: checkForChanges*** LOG addons.xpi: Installed distribution add-on cehomepage@mozillaonline.com*** LOG addons.xpi: Installed distribution add-on cemigration@mozillaonline.com*** LOG addons.xpi: Installed distribution add-on coba@mozilla.com.cn*** LOG addons.xpi: Installed distribution add-on cpmanager@mozillaonline.com*** LOG addons.xpi: Installed distribution add-on livemargins@mozillaonline.com*** LOG addons.xpi: Installed distribution add-on quicklaunch@mozillaonline.com*** LOG addons.xpi: Installed distribution add-on semodifier@mozillaonline.com*** LOG addons.xpi: Installed distribution add-on share_all_cn@mozillaonline.com*** LOG addons.xpi: Installed distribution add-on tabimprovelite@mozillaonline.com*** LOG addons.xpi-utils: Opening database*** LOG addons.xpi-utils: Creating database schema*** LOG addons.xpi: New add-on fxdriver@googlecode.com installed in app-profile*** LOG addons.xpi: New add-on cehomepage@mozillaonline.com installed in app-profile*** LOG addons.xpi: New add-on cemigration@mozillaonline.com installed in app-profile*** LOG addons.xpi: New add-on coba@mozilla.com.cn installed in app-profile*** LOG addons.xpi: New add-on cpmanager@mozillaonline.com installed in app-profile*** LOG addons.xpi: New add-on livemargins@mozillaonline.com installed in app-profile*** LOG addons.xpi: New add-on quicklaunch@mozillaonline.com installed in app-profile*** LOG addons.xpi: New add-on semodifier@mozillaonline.com installed in app-profile*** LOG addons.xpi: Loading bootstrap scope from C:\Users\god\AppData\Local\Temp\anonymous13269webdriver-profile\extensions\semodifier@mozillaonline.com.xpi*** LOG addons.xpi: Calling bootstrap method install on semodifier@mozillaonline.com version 0.2*** LOG addons.xpi: New add-on share_all_cn@mozillaonline.com installed in app-profile*** LOG addons.xpi: New add-on tabimprovelite@mozillaonline.com installed in app-profile*** LOG addons.xpi: New add-on {972ce4c6-7e08-4474-a285-3208198ce6fd} installed in app-global*** LOG addons.xpi: Updating database with changes to installed add-ons*** LOG addons.xpi-utils: Updating add-on states*** LOG addons.xpi-utils: Writing add-ons list*** LOG addons.xpi: Calling bootstrap method startup on semodifier@mozillaonline.com version 0.2*** LOG addons.manager: shutdown*** LOG addons.xpi: shutdown*** LOG addons.xpi-utils: shutdown*** LOG addons.xpi-utils: Database closed*** LOG addons.xpi: startup*** LOG addons.xpi: Skipping unavailable install location app-system-local*** LOG addons.xpi: Skipping unavailable install location app-system-share*** LOG addons.xpi: Ignoring file entry whose name is not a valid add-on ID: C:\Users\god\AppData\Local\Temp\anonymous13269webdriver-profile\extensions\webdriver-staging*** LOG addons.xpi: checkForChanges*** LOG addons.xpi-utils: Opening database*** LOG addons.xpi: No changes found*** LOG addons.xpi: Loading bootstrap scope from C:\Users\god\AppData\Local\Temp\anonymous13269webdriver-profile\extensions\semodifier@mozillaonline.com.xpi*** LOG addons.xpi: Calling bootstrap method startup on semodifier@mozillaonline.com version 0.2*** LOG addons.repository: Recreating database schema*** LOG addons.repository: Creating database schemaat org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:109)at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:245)at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:109)at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:185)at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:178)at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:174)at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:92)at com.org.selenium.Testa.setUp(Testa.java:18)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)at org.junit.runners.ParentRunner.run(ParentRunner.java:300)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

You only need to update the import to the latest selenium package in the Java project. This error occurs when I used 2.25.0, and then download the latest 2.28 on the official website.

How are you doing? It's easy. Come and try it.

References:

Http://seleniumhq.org/docs/

 

 

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.