2017-05-31 119 views
0

在我的應用程序中,我正在處理巨大的兒童窗口。 Windows以遞歸方式進入,如窗口A->單擊A - >窗口B打開 - >單擊B - >窗口C打開 - >單擊C窗口 - >窗口D打開。 所以我在列表中存儲窗口和使用如果,否則,如果條件,以便插入可以維持,但執行代碼時,它切換到所有窗口,有時它不會切換到偉大的子窗口。我的代碼如下所述。如何在硒webdriver中處理巨大的兒童窗口

public class DDE_finnone { 


WebDriver driver; 
WebDriverWait wait; 
String app_id= "1894562"; 

@BeforeTest 
public void setupapplication(){ 

    File filepath = new File("C:\\Automation_Softwares\\IEDriverServer\\IEDriverServer.exe"); 

    System.setProperty("webdriver.ie.driver", filepath.getAbsolutePath()); 

    driver = new InternetExplorerDriver(); 

    java.util.ResourceBundle.clearCache(); 

    wait = new WebDriverWait(driver, 20); 

    driver.get("http://retail.herofincorp.com:7031/finnsso/gateway/SSOGateway?requestID=7000002"); 
} 

@Test 
public void DDE() throws Throwable{ 

    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); 

    /*---------------------------------- CAS MAIN SCREEN ------------------------*/ 

    List<String> browsertabs = new ArrayList<String>(driver.getWindowHandles()); 

    System.out.println(browsertabs); 

    driver.switchTo().window(browsertabs.get(0)); 

    driver.findElement(By.name("TxtUID")).sendKeys("testname"); 

    driver.findElement(By.name("TxtPWD")).sendKeys("Testpwd"); 

    driver.findElement(By.name("DataAction")).click(); 

    // Screenshotpath = fn_screenshot(Screenshotpath); 

    try { 

     if (wait.until(ExpectedConditions.alertIsPresent()) != null) { 

      String alertmessage = driver.switchTo().alert().getText(); 

      System.out.println(alertmessage); 

     } 
    } catch (NoAlertPresentException e) { 
     e.printStackTrace(); 
    } 

    // Screenshotpath = fn_screenshot(Screenshotpath); 

    driver.switchTo().alert().accept(); 

    driver.findElement(By.name("btnCAS")).click(); 

    driver.switchTo().window(browsertabs.get(0)); 

    /*-------------------------- Retail Screen ----------------------*/ 

    List<String> FinnoneCAS1 = new ArrayList<String>(driver.getWindowHandles()); 

    Collections.sort(FinnoneCAS1); 

    System.out.println(FinnoneCAS1); 

    driver.switchTo().window(FinnoneCAS1.get(0)); 

    String title5 = driver.getTitle(); 

    System.out.println(title5); 

    if (driver.getTitle().equalsIgnoreCase("Finnone SSO")) { 

     driver.switchTo().window(FinnoneCAS1.get(1)); 

     System.out.println(driver.getTitle()); 
    } 

    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("frameForwardToApp")); 

    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("banner")); 

    WebElement areasection = driver.findElement(By.id("selBranch")); 

    areasection.clear(); 

    Thread.sleep(1000); 

    areasection.sendKeys("delhi"); 

// Thread.sleep(2000); 

    areasection.sendKeys(Keys.TAB); 

    Thread.sleep(3000); 

// Screenshotpath = fn_screenshot(Screenshotpath); 

    driver.switchTo().parentFrame(); 

    driver.switchTo().frame("contents"); 

    Actions move_AutoLoan1 = new Actions(driver); 

    WebElement Auto_loan1 = driver.findElement(By.linkText("Auto Loan")); 

    move_AutoLoan1.moveToElement(Auto_loan1).build().perform(); 

    WebElement DDEsearch = driver.findElement(By.linkText("Detail Data Entry")); 

    wait.until(ExpectedConditions.visibilityOf(DDEsearch));  

// WebElement allsearch = driver.findElement(By.linkText("All")); 

// wait.until(ExpectedConditions.visibilityOf(allsearch)); 

    DDEsearch.click(); 

    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("main")); 

    WebElement searchappId = driver.findElement(By.id("txtApplication")); 

    wait.until(ExpectedConditions.visibilityOf(searchappId)); 

    // Excel should start 

    searchappId.sendKeys(app_id); 

// Screenshotpath = fn_screenshot(Screenshotpath); 

    Thread.sleep(1000); 

    WebElement searchbtn = driver.findElement(By.name("btnSearch")); 

    wait.until(ExpectedConditions.visibilityOf(searchbtn)); 

    searchbtn.click(); 

    WebElement linkappid = driver.findElement(By.xpath("//table[@class='TABLEATTRIBUTES']/tbody/tr[2]/td[1]/a")); 

    wait.until(ExpectedConditions.visibilityOf(linkappid)); 

    linkappid.click(); 

    driver.switchTo().window(FinnoneCAS1.get(0)); 

    System.out.println(driver.getTitle()); 


    /*-------------- DDE ------------------------------------*/ 

    List<String> DDEscreen = new ArrayList<String>(driver.getWindowHandles()); 

    Collections.sort(DDEscreen); 

    System.out.println(DDEscreen); 

    driver.switchTo().window(DDEscreen.get(2)); 

    System.out.println(DDEscreen.get(2)); 

    String title2 = driver.getTitle(); 

    System.out.println(title2); 

    if (title2.equalsIgnoreCase("Finnone SSO")) { 

     driver.switchTo().window(DDEscreen.get(1)); 

     System.out.println(driver.getTitle()); 

    } else if (driver.getTitle().equalsIgnoreCase("Finnone CAS")) { 

     driver.switchTo().window(DDEscreen.get(0)); 

     System.out.println(driver.getTitle()); 
    } 

    WebElement demographic = driver.findElement(By.id("apy_b0i2text")); 

    wait.until(ExpectedConditions.visibilityOf(demographic)); 

    demographic.click(); 

    WebElement customername = driver.findElement(By.xpath("//table[@class='TABLEATTRIBUTES']/tbody/tr[2]/td[1]/a")); 

    wait.until(ExpectedConditions.visibilityOf(customername)); 

    customername.click(); 

    WebElement personaltab = driver.findElement(By.id("apy_b1i1font")); 

    wait.until(ExpectedConditions.visibilityOf(personaltab)); 

    System.out.println(personaltab.getText()); 

    WebElement qualification = driver.findElement(By.id("selEduQualification")); 

    wait.until(ExpectedConditions.visibilityOf(qualification)); 

    qualification.sendKeys("Graduate"); 

    qualification.sendKeys(Keys.TAB); 

    /*------------ Education window ------------*/ 

    List<String> educationwindow = new ArrayList<String>(driver.getWindowHandles()); 

    Collections.sort(educationwindow); 

    System.out.println(educationwindow); 

    driver.switchTo().window(educationwindow.get(3)); 

    String title6 = driver.getTitle(); 

    System.out.println(title6); 

    if (title6.equalsIgnoreCase("1894562-----NISHANT  OMAR")) { 

     driver.switchTo().window(educationwindow.get(0)); 

     System.out.println(driver.getTitle()); 

    } else if (driver.getTitle().equalsIgnoreCase("Finnone SSO")) { 

     driver.switchTo().window(educationwindow.get(1)); 

     System.out.println(driver.getTitle()); 

    } else if (driver.getTitle().equalsIgnoreCase("Finnone CAS")) { 

     driver.switchTo().window(educationwindow.get(2)); 

     System.out.println(driver.getTitle()); 
    } 

    WebElement searchcriteriaGo = driver.findElement(By.name("B1")); 

    wait.until(ExpectedConditions.visibilityOf(searchcriteriaGo)); 

    searchcriteriaGo.click(); 

    driver.switchTo().window(educationwindow.get(0)); 

    /*-----------------Return from Education Window -----------------------*/ 

    List<String> returneducationdetails = new ArrayList<String>(driver.getWindowHandles()); 

    Collections.sort(returneducationdetails); 

    System.out.println(returneducationdetails); 

    driver.switchTo().window(returneducationdetails.get(2)); 

    System.out.println(returneducationdetails.get(2)); 

    String title21 = driver.getTitle(); 

    System.out.println(title21); 

    if (title21.equalsIgnoreCase("Finnone CAS")) { 

     driver.switchTo().window(returneducationdetails.get(0)); 

     System.out.println(driver.getTitle()); 

    } else if (driver.getTitle().equalsIgnoreCase("Finnone SSO")) { 

     driver.switchTo().window(returneducationdetails.get(1)); 

     System.out.println(driver.getTitle()); 
    } 

} 
+0

您能否考慮提供以下信息:1.硒版本? 2. IEDriverServer版本? 3. IE版本? 4.錯誤堆棧跟蹤。謝謝 – DebanjanB

回答

0

在Selenium中,在窗口中沒有任何層次結構。父母和孩子的窗戶都是按照我們的慣例命名的。但從硒的角度來看,一切都只是另一個窗口。

下面的代碼給出了一些想法,

//Get the current window address 
Set<String> parent = driver.getWindowHandle(); 

//Get the all window addresses that are opened by selenium. This includes the parent window too 
Set<String> all = driver.getWindowHandles(); 


//Assume this is the element you need to interact and it is present in some window. 
String XPATH = ".//"; 

//Now iterate through all windows and search for the element you need to interact with. 
for(String now:all) 
{ 

    driver.switchTo().window(now); 
    if(driver.findElements(By.xpath(XPATH)).size()>0) 
    { 
     System.out.println("Element found in the window "+now+ " "+driver.getTitle()); 
     //DO YOUR ACTUAL STUFF 
    } 
} 

希望這有助於你。謝謝。

+0

Thankyou Santhosh,但我認爲List比Sets更可靠。 –

+1

對於窗口句柄...設置是首選,因爲列表可能有重複但設置不會有任何重複。 –

+0

我同意列表可能會存儲重複的值,但對於Windows ..窗口ids /代碼將在運行時生成唯一的,所以當我們可以保存窗口作爲列表超過3個窗口至少保持插入和輸出順序,我的腳本正在使用這個邏輯,但有些時候,由於7-8遞歸子窗口即將出現故障。我使用了Set,但是這不起作用,就像List正在工作一樣,請爲我提供另一種解決方案,以便始終可以成功執行相同的腳本。 –