如何通过使用框架处理Selenium Web驱动程序TestNg中的子窗口? [英] How to Handle the Child Window in the Selenium Web Driver TestNg by Using Frame Work?
问题描述
在此代码中,我正在使用Single类和Functional类,在这里AllpagesLogins是类名,并且此代码在Functional类中编写,Total Scripit在TestNg类中运行.因此,当我单击linkBtn名称为matrimonials@shaadi.com时 它在那个子窗口关闭链接"按钮中显示子窗口,所以我也用直接代码编写了子窗口代码,但是没有人不能正常工作吗?我该如何处理此子窗口?
In this code i am using the Single class and the Functional Class, Here AllpagesLogins it is a Class Name and the this Code is Writing in the Functional Class, Overall Scripit is running in the TestNg Class. So when i click on the linkBtn Name as matrimonials@shaadi.com it is displaying the Child Window in that Child Window Close Link Button Is There, So i am write the Code for the Child Window Code also and by Direct Code, But no one is not Working Correctly? How can i handle this Child Window?
package EmailMessage;
import java.util.Set;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import Class.AllpagesLogins;
//Declaring the Class Name
public class EmailMessage
{
public void EMSG(WebDriver driver) throws Exception
{
//Here AllpagesLogins is a Class Name
AllpagesLogins APL = new AllpagesLogins();
//Clicking on the Email Message Menu Button
driver.findElement(By.id(APL.EMSG_EmailMsg_MenuBtn_ID)).click();
System.out.println("Clicked on the Email Message Menu Button");
Thread.sleep(2000);
//Checking the Text for the Email Message Title
WebElement asserta=driver.findElement(By.id(APL.EMSG_EmailMsg_MainTitle_ID));
String a=asserta.getText();
try
{
Assert.assertEquals("Email Messages", a);
System.out.println("Main Title Name as Email Messages");
}
catch (Error e1)
{
System.out.println("Wrong Title");
}
//Checking the Text for the Sub Label
WebElement assertb=driver.findElement(By.id(APL.EMSG_EmailMsg_SubLbl_ID));
String b=assertb.getText();
try
{
Assert.assertEquals("Click on the Sender Email to view its details.", b);
System.out.println("Main Title Name as Sub Label");
}
catch (Error e1)
{
System.out.println("Wrong Title");
}
//Clicking on the Child Window
//getting parent Id
String Currenthandle=driver.getWindowHandle();
System.out.println("parent window id:"+Currenthandle);
//handle the child window
Set<String> handles= driver.getWindowHandles();
handles.remove(Currenthandle);
//performing action on child window
driver.switchTo().window(handles.iterator().next());
Thread.sleep(3000);
//Clicking on the Email Message Menu Button
if(driver.findElement(By.linkText("matrimonials@shaadi.com")).isDisplayed())
{
System.out.println("Link Name is Displaying");
driver.findElement(By.linkText("matrimonials@shaadi.com")).click();
System.out.println("Clicked on the Email Id Link Button");
Thread.sleep(2000);
}
else
{
System.out.println("Link Name is Not Displaying");
}
//Handing the Child Windows
if(driver.findElement(By.name("Message Details")).isDisplayed())
{
System.out.println("Message Details Label is Displaying");
driver.findElement(By.id(APL.EMSG_ChildWindow_Close_LnkBtn_ID)).click();
System.out.println("Clicked on the Close Button in the Child Window");
Thread.sleep(2000);
}
else
{
System.out.println("Message Details Label is Not Displaying");
}
}
}
推荐答案
//after clicking button . switch to the window using this code
Set<String> winids = driver.getWindowHandles();
String mainWindow = winids.iterator().next();
String childWindow = winids.iterator().next();
driver.switchTo().window(childWindow);//this switches to the child window
//after switching perform the operations which u want to perform on the child window
这篇关于如何通过使用框架处理Selenium Web驱动程序TestNg中的子窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!