Java + Selenium important interface

We call these methods Selenium or interface is divided into three categories, one is driver-related, which is operating the browser itself is set, the second element is related, is that some elements of clicks, typing and other operations, the third category are some of the tools supporting classes , such as screenshots, log output, event listeners.

This is an introduction manage () interface under the common method. Selenium from the source file, you can know, manage interfaces such Option was used. The method described in this article, are Option This interface can be found below. Option Interface describes it: An interfacefor managing stuff you would do in a browser menu, so these operations are used to control the browser itself.
 

1, Timeout Interface correlation method described

Timeout interface description text section, here are three common interface.

implicitlyWait(long time, TimeUnit unit);

setScriptTimeout(long time, TimeUnit unit);

pageLoadTimeout(long time, TimeUnit unit);

       implicitlyWait (), this in front introduced, called the implicit time to wait. Let's look at this interface description section, roughly meaning: implicit time waiting in finding elements when setting a maximum time value, if the driver did not find the first page element, then keep looking until consumed settings the maximum amount of time, or not found, then an exception is thrown no element found. If within a set time frame to find the element, then the next line of code, do not need to wait time, which is the implicit meaning of waiting. So, implicit time to wait to find generally used in combination with page elements.

       setScriptTimeout (), the role is set within a specified time, waiting for the end of the asynchronous execution of the script, rather than inside throw an error. This may be used in the implementation of javascript script, we introduced back after performing Selenium-related operations using Javascript to introduce specific use in the method.

        pageLoadTimeout (), literally on page load timeout action is set within the specified time, waiting for the full page load is complete, rather than inside throw an error. We know that if Suman, or too many page elements, such as page rendering done a large area, and will definitely cause slow page loads. The role of the interface is to handle such problems. From this perspective, our previous script to set the time to wait for the sentence should be replaced by this statement is more appropriate, after all, our aim is to wait for the page to finish loading.

package com.ralation.week1;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

public class pageLoadTest {
    //pageLoadTimeout联系

    public static void main(String[] args){
        System.setProperty("webdriver.chrome.driver",".\\tools\\chromedriver.exe");

        WebDriver webDriver = new ChromeDriver();

        webDriver.manage().window().maximize();

        webDriver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

//        webDriver.get("https://www.icloud.com/");
        webDriver.get("https://www.baidu.com");

        webDriver.quit();
    }
}

 

2, window associated with the interface method described

window interface. There are several ways the windows interface is mainly used to control the browser window, such as window size, maximum, minimum, full screen, position. Let's look at what method window interface.

void setSize(Dimension targetSize);
void setPosition(Point targetPosition);
Dimension getSize();
Point getPosition();
void maximize();
void fullscreen();

       Used in front of a maximize (), there is no longer introduced, first we introduce setSize (Dimension targetSize) and getPosition (), here to note down the parameters targetSize this is a class, not an ordinary type of data to look at how to write an automated script on the browser location settings.

3, Navigation Interface correlation method described

This introduction Navigation interfaces, including the usual browser forward, backward, open the URL, refresh the current page operation. In the Navigation interfaces, I can find the following four methods:

void back()

void forward()

void to(String url)

void to(URL url)

void refresh()

       Under the first to explain their role, back () is on the front of the browser address bar to the left of the arrow, commonly called the reverse operation. forward () is in front of the browser address bar to the right of the arrow, generally called forward operating, or go to the next page. to (String url) is to open a new page in the current web page, and this opens a new tab there is a difference, to (URL url) is the same, is the url to be passed as a URL object, the less use here no introduction. refresh () This is the browser refresh button operation, or equivalent to pressing F5.
Examples

package com.ralation.week1;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

java.util.concurrent.TimeUnit import;

public class navigationPractice {

    public static void main(String[] args) throws InterruptedException {


        System.setProperty("webdriver.chrome.driver",".\\tools\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        driver.get("https://www.baidu.com");

        Thread.sleep(1000);

        driver.navigate().to("http://jd.com");

        Thread.sleep(1000);

        driver.navigate().back();

        Thread.sleep(1000);

        driver.navigate().forward();

        Thread.sleep(1000);

        driver.navigate().refresh();
        
        driver.quit();
        
    }

}

 

 

 

Published 17 original articles · won praise 0 · Views 178

Guess you like

Origin blog.csdn.net/qq_37637691/article/details/100069447