关闭浏览器

我们已经知道了如何通过TestNG 来管理我们的测试用例,也知道如何去启动我们想要的浏览器,那么浏览器的平时常用的操作还有哪些呢?

关闭当前窗口(tab)

  1. driver.close()

Demo

  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. import org.testng.annotations.Test;
  4. /**
  5. * Created by 米阳 on 18/9/2017.
  6. */
  7. public class BrowserActionsDemo {
  8. @Test
  9. public void openAndClosedBrowser() throws InterruptedException {
  10. // 设置chromedriver系统变量
  11. System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
  12. // 启动chrome浏览器
  13. WebDriver driver = new ChromeDriver();
  14. // 为了看效果,线程等待5S
  15. Thread.sleep(5000);
  16. // 关闭当前窗口
  17. driver.close();
  18. }
  19. }

关闭所有窗口并退出

  1. driver.quit()

Demo

  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. import org.testng.annotations.Test;
  4. /**
  5. * Created by 米阳 on 18/9/2017.
  6. */
  7. public class BrowserActionsDemo {
  8. @Test
  9. public void openAndClosedBrowser() throws InterruptedException {
  10. // 设置chromedriver系统变量
  11. System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
  12. // 启动chrome浏览器
  13. WebDriver driver = new ChromeDriver();
  14. // 为了看效果,线程等待5S
  15. Thread.sleep(5000);
  16. // 关闭所有窗口,并退出
  17. driver.quit();
  18. }
  19. }

上面两个demo例子,最后看到的都是一样,浏览器关闭了,但事实是。close()是只关闭当前窗口,如果有多个窗口并未能关闭浏览器,我们可以查看到进程中“chromedriver.exe”的进程还在。而quit()则真正的结束测试,关闭所有窗口和浏览器并查看不到“chromedriver.exe”的进程。所以平时我们使用结束测试时,记得一定要使用quit(),避免系统资源被driver进程消耗。

打开网站(输入URL)

  1. get()
  2. driver.navigate().to();

这两个方法效果等同,除了代码量外,没区别,都是打开网站。

Demo

  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. import org.testng.annotations.*;
  4. /**
  5. * Created by 米阳 on 18/9/2017.
  6. */
  7. public class BrowserActionsDemo {
  8. WebDriver driver;
  9. @BeforeMethod
  10. public void openChrome() {
  11. // 设置chromedriver系统变量
  12. System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
  13. // 启动chrome浏览器
  14. driver = new ChromeDriver();
  15. }
  16. @Test
  17. public void getTest() {
  18. // 打开百度首页
  19. driver.get("https://www.baidu.com");
  20. // 等同get()方法
  21. driver.navigate().to("https://google.com");
  22. }
  23. @AfterMethod
  24. public void closedChrome() throws InterruptedException {
  25. // 为了看效果,线程等待5S
  26. Thread.sleep(5000);
  27. // 关闭所有窗口,并退出
  28. driver.quit();
  29. }
  30. }

我们把TesNG的BeforeMethod和AfterMethod加到这个demo中来,再每个Test执行之前先去打开Chrome浏览器,Test执行之后等待5S钟关闭浏览器。
Demo中的Test我们通过调用WebDriver的get()方法并传入百度地址打开了百度首页。
注意:
1. 在调用get()方法时,传入的地址不能缺少协议,如http,https不能缺少。
2. get()方法一定会等页面加载完成

浏览器后退操作

  1. driver.navigate().back();

Demo

  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. import org.testng.annotations.*;
  4. /**
  5. * Created by 米阳 on 18/9/2017.
  6. */
  7. public class BrowserActionsDemo {
  8. WebDriver driver;
  9. @BeforeMethod
  10. public void openChrome() {
  11. // 设置chromedriver系统变量
  12. System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
  13. // 启动chrome浏览器
  14. driver = new ChromeDriver();
  15. }
  16. @Test
  17. public void backTest() throws InterruptedException {
  18. // 打开百度首页
  19. driver.get("https://www.baidu.com");
  20. // 等同get()方法
  21. driver.navigate().to("https://google.com");
  22. // 为了看清楚效果,等待2S
  23. Thread.sleep(2000);
  24. // 浏览器后退
  25. driver.navigate().back();
  26. }
  27. @AfterMethod
  28. public void closedChrome() throws InterruptedException {
  29. // 为了看效果,线程等待5S
  30. Thread.sleep(5000);
  31. // 关闭所有窗口,并退出
  32. driver.quit();
  33. }
  34. }

浏览器前进操作

  1. driver.navigate().forward();

Demo

  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. import org.testng.annotations.*;
  4. /**
  5. * Created by 米阳 on 18/9/2017.
  6. */
  7. public class BrowserActionsDemo {
  8. WebDriver driver;
  9. @BeforeMethod
  10. public void openChrome() {
  11. // 设置chromedriver系统变量
  12. System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
  13. // 启动chrome浏览器
  14. driver = new ChromeDriver();
  15. }
  16. @Test
  17. public void backTest() throws InterruptedException {
  18. // 打开百度首页
  19. driver.get("https://www.baidu.com");
  20. // 等同get()方法
  21. driver.navigate().to("https://google.com");
  22. // 为了看清楚效果,等待2S
  23. Thread.sleep(2000);
  24. // 浏览器后退
  25. driver.navigate().back();
  26. // 为了看清楚效果,等待2S
  27. Thread.sleep(2000);
  28. // 浏览器前进
  29. driver.navigate().forward();
  30. }
  31. @AfterMethod
  32. public void closedChrome() throws InterruptedException {
  33. // 为了看效果,线程等待5S
  34. Thread.sleep(5000);
  35. // 关闭所有窗口,并退出
  36. driver.quit();
  37. }
  38. }

页面刷新

driver.navigate().refresh();

Demo

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;

/**
 * Created by 米阳 on 18/9/2017.
 */
public class BrowserActionsDemo {
    WebDriver driver;

    @BeforeMethod
    public void openChrome() {
        // 设置chromedriver系统变量
        System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
        // 启动chrome浏览器
        driver = new ChromeDriver();

    }
    @Test
    public void refreshTest(){
        // 打开百度首页
        driver.get("http://www.baidu.com");
        // 刷新当前页面
        driver.navigate().refresh();
    }
    @AfterMethod
    public void closedChrome() throws InterruptedException {
        // 为了看效果,线程等待5S
        Thread.sleep(5000);
        // 关闭所有窗口,并退出
        driver.quit();
    }
}

浏览器窗口大小设置

最大化

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

设置大小

Dimension dimension = new Dimension(900, 800);
    driver.manage().window().setSize(dimension);

获取大小

driver.manage().window().getSize();

设置窗口位置

Point point = new Point(500, 600);
    driver.manage().window().setPosition(point);

获取窗口位置

driver.manage().window().getPosition();

全屏

driver.manage().window().fullscreen();

Demo

import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;

/**
 * Created by 米阳 on 18/9/2017.
 */
public class BrowserActionsDemo {
    WebDriver driver;

    @BeforeMethod
    public void openChrome() {
        // 设置chromedriver系统变量
        System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
        // 启动chrome浏览器
        driver = new ChromeDriver();

    }

    @Test
    public void windowTest() throws InterruptedException {
        // 设置浏览器大小
        Dimension dimension = new Dimension(900, 800);
        driver.manage().window().setSize(dimension);
        // 为了看清楚效果,等待2S
        Thread.sleep(2000);

        // 获取窗口大小
        Dimension dimension1 = driver.manage().window().getSize();
        int h = dimension1.getHeight();
        int w = dimension1.getWidth();
        System.out.println("h:" + h + "w:" + w);
        // 为了看清楚效果,等待2S
        Thread.sleep(2000);

        // 最大化
        driver.manage().window().maximize();
        // 为了看清楚效果,等待2S
        Thread.sleep(2000);

        // 设置窗口位置,相对屏幕左下角
        Point point = new Point(500, 600);
        driver.manage().window().setPosition(point);
        // 为了看清楚效果,等待2S
        Thread.sleep(2000);

        // 获取窗口位置,相对屏幕左下角
        Point point1 = driver.manage().window().getPosition();
        int x = point1.getX();
        int y = point1.getY();
        System.out.println("x:" + x + "y:" + y);
        // 为了看清楚效果,等待2S
        Thread.sleep(2000);

        // 全屏
        driver.manage().window().fullscreen();
    }

    @AfterMethod
    public void closedChrome() throws InterruptedException {
        // 为了看效果,线程等待5S
        Thread.sleep(5000);
        // 关闭所有窗口,并退出
        driver.quit();
    }
}

控制台输出:

5-1-浏览器常用操作 - 图1

注意:如果遇到无法操作浏览器,这时得检查下driver版本和浏览器版本是否匹配

获取URL 地址

driver.getCurrentUrl();

该方法返回一个String类型。

Demo

import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
 * Created by 米阳 on 18/9/2017.
 */
public class BrowserActionsDemo {
    WebDriver driver;

    @BeforeMethod
    public void openChrome() {
        // 设置chromedriver系统变量
        System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
        // 启动chrome浏览器
        driver = new ChromeDriver();

    }

    @Test
    public void getURLTest() {
        driver.get("https://www.baidu.com");
        String url = driver.getCurrentUrl();
        System.out.println("获取到的URL:" + url);
    }

    @AfterMethod
    public void closedChrome() throws InterruptedException {
        // 为了看效果,线程等待5S
        Thread.sleep(5000);
        // 关闭所有窗口,并退出
        driver.quit();
    }
}

控制台输出:

5-1-浏览器常用操作 - 图2