Selenium first Lesson (Selenium+java+testng+maven)

Source: Internet
Author: User
Tags seleniumhq testng

Selenium Introduction and environment construction

First, Brief introduction

1.selenium:seleniumis one forWebTools for application testing. SeleniumThe test runs directly in the browser, just as the real user is doing. Supported browsers includeIE,Mozilla Firefox,Chromeand so on. Supports automatic recording actions and automatic generation,Net,Java,Pythontest scripts in different languages. SeleniumTestScriptcan be inWindows,Linuxand theMacintosHrunning on multiple platforms.

2.TestNG: TESTNG is a test framework that is inspired by JUnit and NUnit, but introduces some new features that make it more powerful and easier to use. TestNG is an open source automation testing framework; TestNG represents the next generation. TestNG is similar to JUnit (especially JUnit 4), but it is not a junit extension. It was inspired by JUnit. It is designed to outperform JUnit, especially when testing an integrated class. The creator of TestNG is Cedric Beust (Cedric Boystedt)

3.maven: In the process of developing the project, we will use some open source frameworks, third-party tools and so on, which are referenced by the project in the way of jar packages, and some jar packages will also depend on other jar packages, we also need to add to the project, All of these related jar packages will be dependent on the project. Typically, a Java EE project relies on a lot of jar packages. However, this is not a major problem, and the version of the jar package is often the most vexing issue during the process of managing these jar packages. Select a version of the jar package, consider whether the jar it depends on supports this version, and also confirm that the jar that depends on it can be compatible with this version. So, in the course of a previous build project, when you sort out a bunch of jar packages that will make the project work properly, the Lib directory becomes a restricted area. The jar package version is updated, and we seldom bother to touch it. As to whether there is a redundant jar package? It's OK to run properly ....

The advent of MAVEN solves this challenge in the development process. It can manage the project-dependent jar packages, keep your project basic, eliminate redundant jar packages, and make it easy for you to upgrade dependent jar packages. These are just the basic features of MAVEN, which can be used to build projects such as cleaning, compiling, testing, packaging, publishing, and so on.

Second, Selenium+java environmental construction

1. Download and install the JDK

JDKhttp://www.oracle.com/technetwork/java/javase/downloads/index.html

Download step, select Jdk--download

Click Accept, select Windows version download, my computer is a 64-bit system, so I download Jdk-8u101-windows-x64.exe version

Install after download, all the way to hit ' next ',OK. Configure environment variables when installation is complete:

system variables → New java_home variable.

variable Value fill in the JDK installation directory (I am E:\Java\jdk1.8.101)

System Variables → find Path variable → edit

enter%java_home%\bin;%java_home%\jre\bin at the end of the variable value ;

system variables → New CLASSPATH variable

variable values are filled in.; %java_home%\lib;%java_home%\lib\tools.jar

Verify that the configuration is successful run the cmd input java-version

2. Download Eclipse

: https://www.eclipse.org/downloads/eclipse-packages/

I downloaded the Mars version, after downloading, unzip to the local disk, decompression can.

3. Install testng Online

   in Eclipse , click Help, Install new software , and in the add field, enter http://b Eust.com/eclipse, you will see TestNG below . Select Click Install, press Next until the installation is complete , Online installation can be a bit slow.

4.maven Installation

:http://maven.apache.org/download.cgi

The latest version in the download, unzip to the local disk.

To configure environment variables:

Add a new system environment variable Maven_home, and set its value to the directory you installed maven_home= D:\Softwares\apache-maven-3.3.9

Update System PATH variable, add ;%maven_home%\bin

Test Whether the Maven configuration successfully opened a command-line window, enter mvn-v, and if there is a maven version information output proves the configuration was successful, Otherwise, see if your configuration path is correct.

after the Maven installation succeeds, the first command line input maven-help-plugin will be generated under C:\Users\ user \ the. M2 file, by default, drops the loaded jar package called the maven repository.

most maven users need to copy the m2_home/conf/settings.xml file to the . m2 file. Modify the setting.xml. Join in <setting>

<localRepository>D:\eclipse\maven\repository</localRepository>

This allows the newly downloaded jar package to be downloaded to the D:\eclipse\maven\repository path without downloading it to the C Drive every time.

5. New Test Project

Open Eclipse-file-new-other, Select Maven Project and click Next

Select Maven-archetype-quickstar, click Next

Enter the group ID and artifact ID and click Finish.

After the project is built, open the project's Pom.xml (this configuration file, manage Maven's jar packages)

In the configuration file, delete the

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

This is JUnit's jar package, because we don't use it, so delete it.

Join Selenium's dependency

<dependency>

<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>[2.53.0,) </version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>[2.53.0,) </version>
</dependency>

6. Select Browser, I like to use Chrome

Download chrome:https://www.google.com/chrome/browser/desktop/index.html to download chrome for your system

Download chromedriver:https://sites.google.com/a/chromium.org/chromedriver/

Place the chromedriver under Chrome under the installation directory and add the Chromedriver directory to path

For example: My driver path, add C:\Users\hustar\AppData\Local\Google\Chrome\Application to path.

The above based on the Selenium+testng+maven environment is completed, let's write a small program to run a bit

Right-click on the package name to select New-other

Select TestNG class and click Next

Fill in the Class name, select Bforemethod and Aftermethod, click Finish

Write the following code in Beforemethod in Helloword

public class helloword{

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 Chromedriver ();

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

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

Element.sendkeys ("Hello selenium!");

Element.submit ();

}

}

Run the test on the project, or on the class, right-click Run as-testng test.

All rights reserved, welcome reprint, reprint please indicate source: http://www.cnblogs.com/hustar0102/p/5885115.html

Selenium first Lesson (Selenium+java+testng+maven)

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.