preface
As a newcomer, I plan to write a blog to record my personal learning process.
It took a week to learn selenium automation, and then record some syntax, code and so on.
The writing style is not very good. Some code labels and comments may not be very accurate. I hope you will forgive me.
selenium automated testing? I use Java+maven and the tool is IDEA. I use eclipse when learning automation. I'm used to using IDEA to see my personal preferences. (Java programming foundation is required. Java may be launched later, but it has not been written yet)
introduce
Automated testing, as its name implies, uses machine software instead of manually executing test cases.
Automatic test classification: divided into automatic function, automatic interface and automatic unit test.
Premise of automatic test: the demand is relatively stable. After the manual test is passed, the project cycle is long and the script reusability is strong.
Build environment
1, For the Java-jdk1.8 version, please search for the specific installation steps, which will not be described in detail here. Environment variables should be configured and new Java should be created_ HOME,Path,%JAVA_HOME\bin. After completion, you can go to the dos window (win+r, enter cmd) and enter Java, javac and java -version to see if the installation is successful.
2, I use IDEA as the development tool. I can download it directly from the official website: IntelliJ IDEA: The Capable & Ergonomic Java IDE by JetBrains
3, maven, I use version 3.6.3. Download it directly from the official website: Maven – Maven Releases History
Note that the JDK must be installed and configured before use. Here is a quote from a big man: The most complete Maven installation tutorial in history_ Blog of little Du ape - CSDN blog
Go to the dos window and enter mvn and mvn -v to check whether the installation is successful
4, Associate with IEDA
file-->settings-->maven
5, Browser, I use Firefox browser. There are two points: it must be the installation version and the default installation path. Otherwise, an error will be reported
6, The driver, geckodriver, can be downloaded from the official website: geckodriver Mirror
Then copy the geckodriver.exe file to D:\apache-maven-3.6.3\bin (your Maven installation path)
Open dos and enter geckodriver
7, After that, the environment is almost finished. The next step is to write automatic test scripts: write code to control the browser, actual script to control the driver, drive to control the browser, and browser to control the web page
Start programming
Create a project: check file -- "new --" project -- "maven --" select maven archetype quic'kstart -- "next --" write your own project name path name -- "and then maven home path select your own path to install maven --" find "
Then wait for loading (Networking required)
Then modify pom.xml to import the required jar file
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.14.0</version> <scope>test</scope> </dependency>
Copy paste
Then save it. A small m will come out in the upper right corner. Just click it
The interface on the left is shown in the figure. If so, the description is OK, and then start coding. Note that all code should be written in the test file, not in main.
Create package (right-click package) and class (right-click class for package name).
For the test website, the server can search it (open source). I use phpfind and Baidu can download it. If I use phpfind, change the port number to 8088Others don't need to be changed. You can start them
First, test whether the website can be opened automatically( http://localhost:8088/)
Then, enter the script to test whether the browser and website can be opened automatically
package org.example.autotest01; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Demo { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://localhost:8088/"); } }
Open successfully! You can see the red sign in the browser input field and the head of the robot, indicating that the machine has been managed
Next, we start to test the login function (because we opened the website for the first time and didn't register, we wrote a script to test the login function, entered a group of wrong user names and passwords, and the expected result is login failure)
On this page, click login, and then copy the URL( http://localhost:8088/index.php?m=user&c=public&a=login )Create a new class (Login.java)
Test whether you can enter the login interface
package org.example.autotest01; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Login { public static void main(String[] args) { // Write a script to test the login function, enter a set of wrong user names and passwords, and the expected result is login failure // Open browser WebDriver driver = new FirefoxDriver(); // Maximize the browser window. Because the window is very small when opening the website, you should maximize the window driver.manage().window().maximize(); //Visit website driver.get("http://localhost:8088/index.php?m=user&c=public&a=login"); } }
Login page test succeeded
Next, start the test, enter the account and password, right-click the input box, select Check (F12 is not allowed, there may be different data, and an error will be reported), and then locate the element (id, name and class). Copy the location element. The element entering the user name is username and the password element is password
// Find the user name and password boxes and enter the corresponding test data driver.findElement(By.id("username")).sendKeys("test123");//findElement find locating element sendKeys input data to the located element driver.findElement(By.id("password")).sendKeys("123456");
Then click the login button and right-click to check the element. We can find that it only has class, so we locate the class element, and then the class is login_btn fl. at this time, we choose (recommend) the first one, that is, login_btn. Otherwise, an error will be reported
// Find and click the login button driver.findElement(By.className("login_btn")).click();//click() click the element to which you want to navigate
Code integration, and then completed the login test function
During the login process, we can find that there will be page Jump (waiting time) during login, so we need to add a mandatory waiting time for him. Adding this code will report an error. Click Add
The purpose of obtaining the login results is to compare. Before that, we first register an account, then log in and copy the website after successful login( http://localhost:8088/index.php?m=user&c=index&a=index )Compare the expected website with the actual website.
//Get operation result sjwz: actual web address String sjwz = driver.getCurrentUrl();//Get the web address in the current browser // Compare with the expected results and take 2 values for comparison //Expected URL is the login URL yqwz: expected URL String yqwz = "http://localhost:8088/index.php?m=user&c=public&a=login"; //sjwz.equals(yqwz) compares whether two strings are consistent. Consistency is true and inconsistency is false if (sjwz.equals(yqwz)){ System.out.println("Login failed, use case passed"); }else { System.out.println("Login succeeded, use case failed---bug"); }
Finally, the code is integrated and tested
package org.example.autotest01; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Login { public static void main(String[] args) throws InterruptedException { // Write a script to test the login function, enter a set of wrong user names and passwords, and the expected result is login failure // Open browser WebDriver driver = new FirefoxDriver(); // Maximize the browser window. Because the window is very small when opening the website, you should maximize the window driver.manage().window().maximize(); //Visit website driver.get("http://localhost:8088/index.php?m=user&c=public&a=login"); // Find the user name and password boxes and enter the corresponding test data driver.findElement(By.id("username")).sendKeys("test123");//findElement find locating element sendKeys input data to the located element driver.findElement(By.id("password")).sendKeys("123456"); // Find and click the login button driver.findElement(By.className("login_btn")).click();//click() click the element to which you want to navigate // Wait for 5s login operation to complete Thread.sleep(5000); //Get operation result sjwz: actual web address String sjwz = driver.getCurrentUrl();//Get the web address in the current browser // Compare with the expected results and take 2 values for comparison //Expected URL is the login URL yqwz: expected URL String yqwz = "http://localhost:8088/index.php?m=user&c=public&a=login"; //sjwz.equals(yqwz) compares whether two strings are consistent. Consistency is true and inconsistency is false if (sjwz.equals(yqwz)){ System.out.println("Login failed, use case passed"); }else { System.out.println("Login succeeded, use case failed---bug"); } } }
Output results
The first time I write a blog, there may be some deficiencies and mistakes. I hope you will understand