127.md 4.9 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 96 97 98 99 100 101 102 103 104
# 9z。 WebDriver –最大化窗口

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

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

屏幕截图可以节省生命,为了在抓取时查看所有Web元素,最大化浏览器窗口非常重要。 因此,与其向下滚动到特定元素,不如最大化窗口并完成手头的任务。

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

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

## <u>场景</u>

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

此方案的JUnit代码是,

```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
```

## *执行结果:*

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

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

![](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20819%20391'%3E%3C/svg%3E)

<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>