2017-05-31 62 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正在工作一样,请为我提供另一种解决方案,以便始终可以成功执行相同的脚本。 –

相关问题