127.md 4.8 KB
Newer Older
W
wizardforcel 已提交
1
# 9z。 WebDriver – 最大化窗口
W
init  
wizardforcel 已提交
2 3 4

> 原文: [https://javabeginnerstutorial.com/selenium/9z-webdriver-window-maximize/](https://javabeginnerstutorial.com/selenium/9z-webdriver-window-maximize/)

W
wizardforcel 已提交
5
嗨冠军! 事情并非总是以我们希望的方式运作,这就是挑战的形式。 使用 Selenium Webdriver,我们的测试工作变得比我们期望的要容易得多。 一种这样的情况是最大化浏览器窗口。
W
init  
wizardforcel 已提交
6

W
wizardforcel 已提交
7
屏幕截图可以节省生命,为了在抓取时查看所有 Web 元素,最大化浏览器窗口非常重要。 因此,与其向下滚动到特定元素,不如最大化窗口并完成手头的任务。
W
init  
wizardforcel 已提交
8 9 10 11 12 13 14 15

```java
// Maximize the new window
driver.manage().window().maximize();
```

你相信吗? 这一条线就是您想要的! 很难消化? 别担心。 让我们看一个例子,看看这段代码的实际效果。

W
wizardforcel 已提交
16
## 场景
W
init  
wizardforcel 已提交
17

W
wizardforcel 已提交
18
1.  打开 Firefox 浏览器
W
init  
wizardforcel 已提交
19 20
2.  导航到[演示站点](https://chandanachaitanya.github.io/selenium-practice-site/)
3.  获取当前的窗口句柄
W
wizardforcel 已提交
21
4.  使用 ID 定位“单击以打开一个小窗口!”按钮
W
init  
wizardforcel 已提交
22 23 24 25 26 27 28
5.  点击按钮打开小窗口
6.  获取两个打开的窗口的窗口句柄
7.  通过两个句柄循环
8.  切换到带有手柄参考的新窗口
9.  获取标题并将其打印到控制台
10.  将小窗口最大化到全屏尺寸
11.  关闭新视窗
W
wizardforcel 已提交
29 30
12.  将控件切换回父窗口,然后将 URL 打印到控制台
13.  验证 Eclipse IDE 控制台输出屏幕和 JUnit 窗格是否成功
W
init  
wizardforcel 已提交
31

W
wizardforcel 已提交
32
此方案的 JUnit 代码是,
W
init  
wizardforcel 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

```java
package com.blog.junitTests;

import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WindowMaximize {
    // Declaring variables
    private WebDriver driver;
    private String baseUrl;

    @Before
    public void setUp() throws Exception {
        // Selenium version 3 beta releases require system property set up
        System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\"
                + "Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");
        // Create a new instance for the class FirefoxDriver
        // that implements WebDriver interface
        driver = new FirefoxDriver();
        // Implicit wait for 5 seconds
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        // Assign the URL to be invoked to a String variable
        baseUrl = "https://chandanachaitanya.github.io/selenium-practice-site/";
    }

    @Test
    public void testPageTitle() throws Exception {
        // Open baseUrl in Firefox browser window
        driver.get(baseUrl);        
        // Get current window handle
        String parentWinHandle = driver.getWindowHandle();
        // Locate 'Click to open a small window!' button using id
        WebElement newWindowBtn = driver.findElement(By.id("win2"));
        // Click the button to open a new window
        newWindowBtn.click();
        // Get the window handles of all open windows
        Set<String> winHandles = driver.getWindowHandles();
        // Loop through all handles
        for (String handle : winHandles) {
            if (!handle.equals(parentWinHandle)) {
                driver.switchTo().window(handle);
                System.out.println("Title of the new window: " + driver.getTitle());
                // Maximize the new window
                driver.manage().window().maximize();
                driver.close();
            }
        }
        // Switching the control back to parent window
        driver.switchTo().window(parentWinHandle);
        // Print the URL to the console
        System.out.println("Parent window URL: " + driver.getCurrentUrl());

    } // End of @Test
```

W
wizardforcel 已提交
96
## 执行结果:
W
init  
wizardforcel 已提交
97 98 99

清晰的注释使代码不言自明。

W
wizardforcel 已提交
100
在“Eclipse IDE 中 -> JUnit 窗格 -> 绿色条”显示测试用例已成功执行。 控制台窗口显示没有任何错误。 它还按预期显示所有打印的消息。
W
init  
wizardforcel 已提交
101

W
wizardforcel 已提交
102
<noscript><img alt="" class="alignnone size-full wp-image-12549" height="391" src="img/8747d9f868b65e6dad7be604914dd691.png" width="819"/><p>如有任何疑问,请不要在评论部分大喊大叫!</p><p>我很快会在另一篇文章中再见。 祝你有美好的一天!</p><div class="sticky-nav" style="font-size: 15px;"><div class="sticky-nav-image"></div><div class="sticky-nav-holder"><div class="sticky-nav_item"><h6 class="heading-sm">下一篇文章</h6></div><h5 class="sticky-nav_heading " style="font-size: 15px;"><a href="https://javabeginnerstutorial.com/selenium/9aa-webdriver-executing-javascript-code/" title="9aa. WebDriver – Executing JavaScript code"> 9aa。 WebDriver – 执行 JavaScript 代码</a></h5></div></div> </body> </html></noscript>