Have you heard of Selenium?

1.1 Automated Testing

When it comes to Selenium, automated testing is essential.

Automated testing, is the manual test process, into the machine automatic test process.

Automated testing has the following advantages

  • Regression testing of programs is more convenient. This is probably the most important task of automated testing, especially when the program changes frequently. .
  • You can run more and more tedious tests. .
  • You can perform tests that are difficult or impossible to do manually. .
  • Better use of resources. .
  • Tests are consistent and repeatable. .
  • Reusability of tests. .
  • Increase software trust.

1.2 the Selenium

Selenium is one of the best automated testing tools that has emerged in pursuit of a great automated testing experience.

Selenium is a tool for Web application testing. It is a browser emulator that can operate the browser as if it were a real user.

Selenium supports all major browsers and programming languages, including Java, Python, C#, PHP, Ruby, JavaScript, etc. It is based on standard WebDriver syntax specifications and supports automation of all web-based management tasks.

Selenium consists of several software tools. Each tool has a specific role. The following tools are available:

  • Selenium IDE (Integrated Development Environment) is a prototyping tool for building test scripts
  • Selenium RC is the remote control of Selenium (also called Selenium1.0).
  • Selenium Grid enables test sets to run test cases in parallel across multiple environments.

Selenium is integrated in Java

Selenium supports major programming languages, including Java, Python, C#, PHP, Ruby, and JavaScript.

Q: Why Java over Python?

A: Python is A succinct and efficient scripting language. I’ll write another Python version sometime.

2.1 Maven Adds dependencies

Using Selenium in Java is simple, you just need to add the following dependencies:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>23.0</version>
</dependency>
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.2</version>
</dependency>
Copy the code

2.2 Adding a Browser driver

When Selenium was upgraded to 3.0, different browser drivers were standardized. If you want to use Selenium to drive different browsers, you must download and set up different browser drivers separately.

  • Firefox driver: Geckodriver
  • Chrome driver: Chrome Driver
  • IE browser driver: IEDriverServer
  • Edge browser driver: MicrosoftWebDriver
  • Opera browser driver: Operadriver
  • PhantomJS browser driver: PhantomJS

Using different browsers in Java: First configure the driver properties and specify the driver file path

System.setProperty("webdriver.chrome.driver"."Q:\\chromedriver.exe");
Copy the code

Get the WebDriver and open a new browser window

WebDriver driver = new ChromeDriver();    / / Chrome
WebDriver driver = new FirefoxDriver();   / / the Firefox browser
WebDriver driver = new EdgeDriver();      / / Edge browser
WebDriver driver = new InternetExplorerDriver();  // Internet Explorer
WebDriver driver = new OperaDriver();     / / the Opera browser
WebDriver driver = new PhantomJSDriver();   //PhantomJS
Copy the code

Note: You can use windowless mode in Linux, which will be covered later

A simple sample

public class Itest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver"."Q:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.ytooo.org");
  
        Thread.sleep(10000); driver.close(); }}Copy the code

2.2 Selenium element localization

2.2.1 Locating elements
  • findElement(By.id()) driver.findElement(By.id(“kw”))

  • findElement(By.name()) driver.findElement(By.name(“wd”))

  • findElement(By.className()) driver.findElement(By.className(“s_ipt”))

  • findElement(By.tagName()) driver.findElement(By.tagName(“input”))

  • findElement(By.linkText())

<a class="mnav" href="http://news.baidu.com" name="tj_trnews">news</a>
<a class="mnav" href="http://www.hao123.com" name="tj_trhao123">hao123</a>
Copy the code
Driver.findelement (by.linkText ("hao123") driver.findelement (by.linkText ("hao123")Copy the code
  • findElement(By.partialLinkText())

Driver. FindElement (By partialLinkText (” new “)

  • findElement(By.xpath()) driver.findElement(By.xpath(“//*[@id=’kw’]”))
  • findElement(By.cssSelector()) driver.findElement(By.cssSelector(“html > body > form > span > input”)
2.2.2 Obtaining the element list
    driver.findElements(By.cssSelector(".for.list td"));
Copy the code

The obtained element List is a List object. It is not recommended to loop directly to obtain the element object. Instead, it is recommended to retrieve the element object from the root to avoid failure to obtain the element

List<WebElement> heads = driver.findElements(By.cssSelector(".for.list td"));

for (int i = 0; i < heads.size(); i++) {
    String href = driver.findElements(By.cssSelector(".for.list td")).get(i).getText();
}
Copy the code
2.2.3 Select from the drop-down list
WebElement el = driver.findElement(By.xpath("//select"));
Select sel = new Select(el);
sel.selectByValue("20");
Copy the code

2.3 Setting Element Wait

WebDriver provides two types of waits: explicit and implicit.

2.3.1 Explicit Wait

Wait explicitly, for an element

WebDriverWait wait = new WebDriverWait(driver,10.1);
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".for.list")));
Copy the code
2.3.1 Explicit Wait

Implicit waiting, waiting for an element

driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
Copy the code

2.4 WebElement Common Methods * clear() Clears text.

  • SendKeys (*value) Simulates key input.
  • Click () clicks on the element
driver.findElement(By.id("username")).sendKeys("Username");
driver.findElement(By.id("password"))sendKeys("Password");
driver.findElement(By.id("commit")).click;
Copy the code

2.5 Keyboard and mouse operation

2.5.1 Actions Keyboard and mouse operations
  • Right-click contextClick ()
  • ClickAndHold () Mouse click and control
  • Double-click the doubleClick ()
  • DragAndDrop (drag)
  • Release () releases the mouse
  • Perform () performs all Actions stored in Actions
// Create an action
Actions action = new Actions(driver);   
// Left mouse click
action.click().perform();
// Double click the left mouse button
action.doubleClick(WebElement).perform();
// Left mouse button down
action.clickAndHold(WebElement).perform();
// Mouse over the element
action.moveToElement(WebElement).perform();
// Right-click the element
action.contextClick(WebElement).perform();
// Drag the target element onto the specified element
action.dragAndDrop(webElement1,webElement2);
action.dragAndDrop(webElement, xOffset, yOffset);

Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL);// Press the Control key
action.keyUp(Keys.CONTROL);// Release the control key
action.keyDown(Keys.CONTROL).keyDown(Keys.ALT).keyDown("A").keyUp(Keys.CONTROL).keyUp(Keys.ALT).keyUp("A").perform();
action.sendKeys(Keys.CONTROL+"a").perform();
action.sendKeys(Keys.CONTROL, Keys.ALT, "A").perform();
Copy the code
2.5.2 element sendKeys ()
SendKeys (keys.back_space) BackSpace (keys.space) SPACE (sendKeys(keys.tab) TAB (TAB)Copy the code

2.6 Window Control

2.6.1 Window Switching

Get all Windows using the driver.getwindowhandles () method

Use driver.switchto ().window(hand) to switch the window

Set<String> handles = driver.getWindowHandles();
for (String hand : handles) {
    if (!StringUtils.equals(mainHand, hand)) {
        driver.switchTo().window(hand);
    }
}
Copy the code

3 Windowless mode in Linux

3.1 Installing Chrome for Linux

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm 
yum install -y google-chrome-stable_current_x86_64.rpm
Copy the code

3.2 Downloading the Driver of the corresponding version

Example Query the current browser version

google-chrome --version
Copy the code

3.3 Setting the Selenium headless mode

  1. Set headless mode
options.setHeadless(Boolean.TRUE);
Copy the code
  1. Configuration header information
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
Copy the code
  1. The browser window size needs to be configured to ensure that elements are retrievable
WebDriverWait wait = new WebDriverWait(driver, 60);
Dimension dimension = new Dimension(1920.1080);
driver.manage().window().setSize(dimension);
Copy the code
  1. If the following information is displayed
The driver is not executable: /opt/code/news/chromedriver
Copy the code

Run the following command:

chmod 775 ./chromedriver
Copy the code

3.4 If the setting succeeds, the system starts running

For more interesting and interesting content, welcome to my blog to communicate and improve WaterMin together