硒|将已打开的浏览器(IE)附加到webdriver

时间:2016-07-28 09:56:01

标签: java selenium selenium-webdriver

我正致力于网站的自动化,我需要使用用户A 执行某些操作,然后用户B 需要批准相同的操作。

默认情况下,Web应用程序从Windows登录本身获取凭据,因为没有登录页面。

所以我做的是,我自动完成了用户A 的所有操作。现在要从用户B 执行操作,我创建了一个.vbs实用程序,我在我的java代码中调用,该实用程序打开Web浏览器并使用用户B登录应用程序(I在vbs中使用shell脚本编写)。现在我有两个Web浏览器,它由webdriver打开(我在其上执行了一些操作,用户A ),另一个已经被vbs实用程序打开(我需要对此执行一些操作)浏览器),因为我的第二个浏览器没有被Webdriver打开,我正在寻找一种方法将它附加到webdriver,以便我可以对它执行一些操作&批准用户A 创建的请求。

其他信息: 我需要在IE中执行此操作,因为这是客户端要求。 我正在使用java for selenium。

1 个答案:

答案 0 :(得分:0)

通过阅读您的问题,我相信您正在正确解决您的问题。由于我无法确定您在浏览器中处理的是哪种身份验证,我想我可以向您展示一个表单和HTTP 401,这很可能会涵盖一般情况。如果您需要其他一些示例,请以不同的方式进行身份验证,请告诉我们。

如果我误读了您的请求,并且您实际上想要挂接到默认情况下未提供的手动启动的浏览器实例,则必须真正具有创造性。无论如何,这个问题已经回答here

但是,就我的建议而言,我强烈建议你尽可能摆脱那个vb脚本/手动浏览器。如果您可以通过浏览器进行身份验证,那么您可以通过selenium进行身份验证。以下是一些例子:

Note, this is a good guide on how to use the InternetExplorerDriver

场景1:使用HTML表单 示例HTML代码:

<form>
    <table id="credentials_table">
    <tbody>
    <tr>
        <td class="credentials_table_label_cell"><label for="username" id="label_input_1">Username</label></td>
        <td class="credentials_table_field_cell"><input class="credentials_input_text" value="" id="username" autocomplete="off" autocapitalize="off" type="text"></td>
    </tr>
    <tr>
        <td class="credentials_table_label_cell"><label for="password" id="label_input_2">Password</label></td>
        <td class="credentials_table_field_cell"><input class="credentials_input_password" value="" id="password" autocomplete="off" autocapitalize="off" type="password"></td>
    </tr>
    <tr id="submit_row">
        <td class="credentials_table_field_cell"><input class="credentials_input_submit" value="Logon" type="submit"></td>
    </tr>
    </tbody></table>
</form>    

要登录的Selenim代码示例:

private WebDriver driverForA = new InternetExplorerDriver();
private WebDriver driverForB = new InternetExplorerDriver();

@After
public void after() {
    driverForA.close();
    driverForB.close();
}

@Test
public void testADoesThisAndBDoesThat() {

    driverForA.get("http://my.login.url");
    final WebElement usernameInput = driverForA.findElement(By.id("username"));
    final WebElement passwordInput = driverForA.findElement(By.id("password"));
    final WebElement submitButton = driverForA.findElement(By.xpath("//input[@type='submit' and @value='Logon']"));
    // perform the login stuff
    clearKeysAndSetValue(usernameInput, "Joe");
    clearKeysAndSetValue(passwordInput, "superSecret");
    submitButton.click();
    // navigate to other pages and do things

    driverForB.get("http://my.approval.page");
    final WebElement approveButton = driverForB.findElement(By.id("approval_Button"));
    approveButton.click();

}

private void clearKeysAndSetValue(final WebElement element, final String valueToSet) {
    element.clear();
    element.sendKeys(valueToSet);
}

Scenario 2: Using HTTP 401

private final WebDriver driverForA = new InternetExplorerDriver();
private final WebDriver driverForB = new InternetExplorerDriver();

@After
public void after() {
    driverForA.close();
    driverForB.close();
}

@Test
public void testADoesThisAndBDoesThatHttpBasic() {
    // authenticate as a
    WebDriverWait wait = new WebDriverWait(driverForA, 5);      
    Alert alert = wait.until(ExpectedConditions.alertIsPresent());     
    alert.authenticateUsing(new UserAndPassword("Joe", "superSecret"));
    // navigate to other pages and do things

    driverForB.get("http://my.approval.page");
    final WebElement approveButton = driverForB.findElement(By.id("approval_Button"));
    approveButton.click();
}