I am a software testing white, in the study of automated testing, really into a lot of pits, until now, may barely build up the environment.
Here, I briefly summarize the Java-based Selenium Automated test environment setup, as a note for those of you who are learning software testing.
However, after proficiency should also not forget, so, when beginning self-study to practice more, small friends!!
1. Install and configure the Eclipse environment
I think you can configure Eclipse in a minute, so I won’t bother you too much
(1) Download the Eclipse installation package from the official website and decompress it.
(2) Download JDK from the official website and configure environment variables:
JAVA_HOME: directory where JDK files are stored. Path: %JAVA_HOME%\bin or C: Program Files\Java\jdk1.8.0_111\binCopy the code
(3) After the configuration is complete, enter Java -version in the CMD console. If the version information is displayed, the configuration is successful.
2. Configure maven environment
The purpose of using Maven is not to import selenium JAR packages, you can add dependencies in Maven POM files, directly download jar packages, convenient to use.
(1) Download maven files as zip files from the official website and decompress the files.
(2) Configure maven environment variables;
MAVEN_HOME: maven file directory Path: %MAVEN_HOME%\binCopy the code
3. Add Maven to Eclipse
(1) Click Window → Preference to enter the dialog box page, and then enter the UserSettings setting page. As shown in the figure, select the absolute path of settings. XML file and local warehouse. Click Apply and OK.
(2) To test whether Maven is successfully installed, open Eclipse and choose “File” – > “New” – > “Project” to create a “Maven Project”. The following figure indicates that the configuration is complete:
4. Add Testng to Eclipse.
The purpose of using Testng is to facilitate the management of the test cases, and to execute the test cases as needed, not all of them. Also, Testng adds dependencies to the POM file so that the test cases can be executed in Jenkins’ POM file.
(1) Open our Eclipse and click Help ->Install New Software, as shown in the picture:
beust.com/eclipse
5. Write code examples
I will briefly introduce my operation steps here.
(1) Create a New Maven project.
My directory structure looks like this
Here is my use case code, with three tests, as can be seen from the comments. Of course, the middle involves calling other classes, calling files, inheriting other classes and other problems, I won’t paste all of them
@test // Open the home page public voidopenPage(){driver.get(" Here is the open address ");waitTime(2000);
driver.manage().window().maximize();
waitTime(2000);
}
@Test(dependsOnMethods={"openPage"}) // Login public voidLogin(){
this.propertiesfile();
WebElement name = driver.findElement(By.id(prop.getProperty("user")));
name.sendKeys(prop.getProperty("uservalue"));
waitTime(1000);
WebElement passwo = driver.findElement(By.id(prop.getProperty("passwo")));
passwo.sendKeys(prop.getProperty("passwovalue"));
waitTime(1000);
WebElement Loginbutton = driver.findElement(By.id(prop.getProperty("login")));
Loginbutton.click();
waitTime(3000);
String e1 = driver.findElement(By.id("login-name")).getText();
System.out.println("The current logon is:"+e1);
}
@Test(dependsOnMethods={"Login"}) // Add vendor public voidAddgys(){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();",driver.findElement(By.linkText("Supplier Management")));
js.executeScript("arguments[0].click();",driver.findElement(By.linkText("Supplier List")));
WebElement iframe = driver.findElement(By.id("/cpkhe/supplier/list"));
driver.switchTo().frame(iframe);
WebDriverWait wait1 = new WebDriverWait(driver,30);
wait1.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(prop.getProperty("addgysbutton"))));
WebElement gys3 = driver.findElement(By.cssSelector(prop.getProperty("addgysbutton")));
gys3.click();
waitTime(2000);
driver.switchTo().defaultContent();
WebElement iframe1 = driver.findElement(By.id("/cpkhe/supplier/add"));
driver.switchTo().frame(iframe1);
driver.findElement(By.id("supName")).sendKeys("Auto Supplier Name 1");;
driver.findElement(By.id("firstContact")).sendKeys("First contact");
driver.findElement(By.id("firstContactPost")).sendKeys("First Contact Position");
driver.findElement(By.id("firstContactMobile")).sendKeys("18892080505");
driver.findElement(By.id("contactEmail")).sendKeys("[email protected]");
driver.findElement(By.id("sourceInfo")).sendKeys("Channel information channel information channel information la la la la");
driver.findElement(By.id("brandName0")).sendKeys("Brand Name 1");
driver.findElement(By.cssSelector(".ant-select-selection__placeholder")).click();
driver.findElement(By.xpath(prop.getProperty("branddowm"))).click();
driver.findElement(By.id("applyReason")).sendKeys("Reason for application");
driver.findElement(By.cssSelector("span.ant-upload > input:nth-child(1)")).sendKeys("C: \ \ Users \ \ ye menghua \ \ Desktop \ \ beauty of red star triumphant dragon \ \ pictures \ \ timg JPG");
waitTime(2000);
driver.findElement(By.cssSelector("button.ant-btn:nth-child(2)")).click();
WebDriverWait wait2 = new WebDriverWait(driver,30);
wait2.until(ExpectedConditions.presenceOfElementLocated(By.className("message-info-desc")));
String text1 = driver.findElement(By.className("message-info-desc")).getText();
System.out.println("After new supplier submission, prompt message:"+text1); } // Wait time public static voidwaitTime(int time){ try { Thread.sleep(time); } catch (InterruptedException e) { e.printStackTrace(); }}Copy the code
(2) All the packages required by the project will be added to the POM file. You only need to search the Maven repository (mvnRepository.com) for the required packages and add them to the POM file.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope> </dependency> <! -- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependency> < the groupId > org. Seleniumhq. Selenium < / groupId > < artifactId > selenium - Java < / artifactId > < version > 2.53.1 < / version > </dependency> <dependency> <groupId>velocity</ artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId> XML -apis</artifactId> XML -apis</artifactId> <version>1.4.01</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId> < version > 6.8.21 < / version > < scope >test</scope> </dependency> <! -- Relies on reportNg for associationtestNg-->
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.2</version>
<scope>test</scope> <exclusions> <exclusion> <groupId>org.testng</groupId> <artifactId>testng</artifactId> </exclusion> </exclusions> </dependency> <! Guice --> <dependency> <groupId>com.google.inject</groupId> <artifactId> Guice </artifactId> <version>3.0</version> <scope>test</scope> </dependency> <! -- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin --> <dependency> . < groupId > org, apache maven plugins < / groupId > < artifactId > maven - surefire - plugin < / artifactId > < version > 2.19.1 < / version > </dependency> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>3.0</version> <scope>test</scope> </dependency> <plugins> <! -- Add plugin, add ReportNg listener, Plugins </groupId> <artifactId> Maven-surefire-plugin </artifactId> <version>2.5</version> <configuration> <properties> <property> <name>usedefaultlisteners</name> <value>false</value> </property> <property> <name>listener</name> <value>org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter</value> </property> </properties> <workingDirectory>target/</workingDirectory> <forkMode>always</forkMode> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> Maven-compiler-plugin </artifactId> <version>3.5</version> <configuration> <encoding>UTF-8</encoding> <source> 1.8 < /source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> < artifactId > maven - surefire plugin < / artifactId > < version > 2.20 < / version > < configuration > < suiteXmlFiles > <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build>Copy the code
6. Generate testng. XML
(1) Right-click the project name, TestNg→Convert to TestNg, and then click Next→ Finish. The testng.xml file is generated.
(4) The generated reports are in the index.html file under the Target \ Surefire-reports \ HTML directory. It should be noted that I added reportng dependencies to my POM file. All reports are styled as follows.
7. Jenkins environment configuration
To set Jenkins up in the local Tomcat, the First step is to set up the Tomcat environment, download the Tomcat compression package from the official website, decompress it, and configure the Tomcat environment variables, then start the startup.bat file in the bin directory of Tomcat, and the window as shown in the figure appears
Download Jenkins. The war file, download after successful, will change files in the tomcat webapp directory, restart tomcat, enter http://localhost:8080/jenkins/ in the address bar to open the Jenkins page.
8. How do Jenkins build the POM file configuration
(1) Click System Administration → Global Tool Configuration to configure JDK and Maven parameters
9. Jenkins will send the report by email after the completion of construction
(1) Configure the following parameters in System Management → System Configuration
(2) Click Configure in the project task, and the configuration content is as follows
According to the operation, is there any success?
conclusion
I can’t find some options in Jenkins, it must be because I haven’t downloaded the corresponding plugins.
I wish you an early success ~~~~~~