2016-07-28 105 views
-1

我在一個網站中,我需要使用用戶需要批准同一個然後用戶B來執行一些操作的自動化工作。硒|附上已經打開的瀏覽器(IE),以webdriver的

默認情況下,Web應用程序從Windows登錄本身獲取憑據,因爲沒有登錄頁面。

所以我做的是,我自動從用戶A的所有行動。現在要執行從用戶B的行動,我創建了一個.vbs實用程序,我打電話給我的java代碼,此實用程序打開一個Web瀏覽器並與用戶B登錄到應用程序(我用vbs爲此shell腳本)。現在我有兩個網頁瀏覽器,一個是由webdriver打開的(我用用戶A執行了一些操作),另一個是由vbs實用程序打開的(我需要在這個瀏覽器上執行一些操作),因爲我的第二個瀏覽器不是由Webdriver打開我正在尋找一種方法將它附加到webdriver,以便我可以對它執行一些操作&批准由用戶A創建的請求。

附加信息: 我需要在IE中執行此操作,因爲這是客戶端要求。 我使用java的硒。

+1

爲什麼不打開一個新的webdriver而是創建一個vbs? – Madhan

回答

0

從閱讀你的問題,我相信你正在解決你的問題不正確。由於我無法確定您在瀏覽器中處理的是哪種身份驗證,因此我想我可以向您展示一個表單和HTTP 401,它很可能涵蓋一般場景。如果您需要其他示例,請以不同的方式進行身份驗證,請告訴我們。

如果我誤解了您的請求,並且您確實想要掛接到手動啓動的瀏覽器實例(默認情況下未提供),您必須非常有創意。無論如何,這個問題的答案是here

但是,據我的建議,我強烈建議您儘可能地去掉vb腳本/手動瀏覽器。如果您可以通過瀏覽器進行身份驗證,則可以通過硒進行身份驗證。下面是一些例子:

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(); 
} 
相關問題