Selenium Automated test environment Construction ECLIPSE+SELENIUM+JUNIT+TESTNG

Source: Internet
Author: User
Tags xpath xpath finder testng

1. Installing the JDK

JDK1.7

Download path: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

Punch ' Next ', OK. Configure environment variables when installation is complete:

Java_home = E:\Java\Java\jdk1.7.0_15

PATH =%java_home%\bin

CLASSPATH =.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar

CMD command-line input:java-version, returns the following result, indicating that the installation was successful

2. Download Eclipse

:http://www.eclipse.org/download/

Once the download is complete, unzip it directly to use it.

3. Download Selenium IDE, SELENIUMRC, Iedriverserver, Seleniumclient Drivers

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

1. Selenium Ide:selenium-ide-2.2.0.xpi is used to record scripts on Firefox.

2, Selenium Rc:selenium-server-standalone-2.33.0.jar simulation server side, not less.

3, Iedriverserver:driverserver_win32_2.33.0.zip IE Drive, Firfox and chorm without driver.

4, Selenium client Drivers:selenium-java-2.33.0.zip analog Selenium clients.

Here, I will download all the files, all stored under E:\eclipse\selenium, easy to manage:


4. Install IDE, Firebug, XPath checker, XPath finder

After installing Firefox, open Firefox, the front download selenium-ide-2.2.0xpi drag and drop to Firefox, pop up, install.

Firebug, XPath checker, XPath Finder, open the Firefox browser, select Tools-Add-ons, open the Add-Ons Manager page, search for Firebug, XPath.

Load the query into the Firebug, XPath Checker, and XPath finder, and take effect after restarting the browser:

Seleniumide, Firebug and XPath usage, can be Baidu selenium private cuisine (beginner's introductory tutorial). PDF, there is a good explanation.

5. Enable SELENIUMRC

Ways to start SELENIUMRC:
CMD command line into the Selenium-server-standalone-2[1].33.0.jar storage directory, enter the following command
Java-jar Selenium-server-standalone-2[1].12.0.jar

For convenience, the startup command can be written with a bat to execute, Run_selenium.bat, as follows:

@echo off

CD E:\eclipse\selenium

E:java-jar Selenium-server-standalone-2.33.0.jar

6. Eclipse executes Java instance of selenium

Open Eclipse, create a new project File-new-java project

Enter project name: Selenum,next

Next, window into Java Settings, select Libraries, click Addlibrary.

A jar package (E:\eclipse\plugins\org.junit_4.11.0.v2XXXX) that references Junit4.

Then click Add External Jars ...,

Referring to selenium related packages (E:\eclipse\selenium), the final libraries is as follows:

When you are finished, the Java view is as follows:

Right-click Src,new->package to create a new package selenium_test,

Then right-click on the package Selenium_test,new->class, create a new class class Case1.java, the final effect is as follows:

Here we use IE browser to execute an instance, modify Case1.java, the code is as follows:

Package selenium_test;

Import Org.openqa.selenium.By;

Import Org.openqa.selenium.WebDriver;

Import org.openqa.selenium.WebElement;

Import Org.openqa.selenium.ie.InternetExplorerDriver;

Import org.openqa.selenium.remote.DesiredCapabilities;

public class Case1 {

public static void Main (string[] args) {

System.setproperty ("Webdriver.ie.driver",

"E:\\eclipse\\selenium\\iedriverserver.exe");//Note the file storage path IEDriverServer.exe here

Desiredcapabilities iecapabilities = desiredcapabilities

. InternetExplorer ();

Iecapabilities

. setcapability (

Internetexplorerdriver.introduce_flakiness_by_ignoring_security_domains,

true);

Webdriver Driver = new Internetexplorerdriver (iecapabilities);

Driver.get ("http://www.google.com.hk");

webelement element = Driver.findelement (By.name ("Q"));

Element.sendkeys ("Hello selenium!");

Element.submit ();

try {

Thread.Sleep (3000);

} catch (Interruptedexception e) {

E.printstacktrace ();

}

System.out.println ("page title is:" + driver.gettitle ());

Driver.quit ();

}

}

Run Run_selenium.bat and start the Selenium RC server.

Then right-click on the Case1.java,run As->java application to perform the following successful results:

Here we run the script through JUnit, and the script needs to be modified because JUnit's Java file has its own format.

7th step Eclipse executes Java instance of selenium through JUnit

Right-click Selenium_test,new->junit Test case to create a new Case2.java.

Complete the following:

The CASE2 code is as follows:

Package selenium_test;

Import org.junit.*;

Import org.openqa.selenium.*;

Import Org.openqa.selenium.firefox.FirefoxDriver;

public class Case2 {

Webdriver driver;

@Before

public void SetUp () throws Exception {

Driver = new Firefoxdriver ();

}

@Test

public void Test_case2 () throws Exception {

Driver.get ("http://www.google.com.hk");

webelement element = Driver.findelement (By.name ("Q"));

Element.sendkeys ("Hello selenium!");

Element.submit ();

}

@After

public void TearDown () throws Exception {

System.out.println ("page title is:" + driver.gettitle ());

Driver.quit ();

}

Run Run_selenium.bat and start the Selenium RC server (if it is not turned off after the front RC starts, you do not need to start more than one).

Right-click Case2.java,run as->junit Test to perform the following successful results:

8th step Eclipse Executes the Java instance of selenium via testng

Installing TestNG

In Eclipse, click Help, install new software, enter Http://beust.com/eclipse in the Add bar, and you'll see TestNG below. Select Click to install, press Next until the installation is complete, the online installation will have The point is very slow.

After installing restart Eclipse, select java->testng in Window->show view->other and the TestNG option will appear.

Right-click Package selenium_test,new->other->testng to create a new TestNG test class Case3.java.

When done, follow

The CASE3 code is as follows:

Package selenium_test;

Import Org.testng.annotations.Test;

Import Org.openqa.selenium.By;

Import Org.openqa.selenium.WebDriver;

Import org.openqa.selenium.WebElement;

Import Org.testng.annotations.BeforeMethod;

Import Org.testng.annotations.AfterMethod;

Import Org.openqa.selenium.firefox.FirefoxDriver;

public class Case3 {

Webdriver driver;

@BeforeMethod

public void Beforemethod () {

}

@AfterMethod

public void Aftermethod () {

System.out.println ("page title is:" + driver.gettitle ());

Driver.quit ();

}

@Test

public void Test_case3 () {

Driver = new Firefoxdriver ();

Driver.get ("http://www.google.com.hk");

webelement element = Driver.findelement (By.name ("Q"));

Element.sendkeys ("Hello selenium!");

Element.submit ();

}

}

Run Run_selenium.bat and start the Selenium RC server.

Right-click Case3.java,run as->testng Test to perform the following successful results:

After execution, a test-output folder is generated, and the index.html under the folder is the test report, as follows:

These are the test environments under Eclipse on how to build selenium, including direct execution. Java, execute. Java through JUnit, execute. Java through testng.

This article is excerpt Selenium Chinese forum group share the information inside, share to everyone

Selenium Automated test environment Construction ECLIPSE+SELENIUM+JUNIT+TESTNG

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.