Selenium webdriver 如何使用框架处理SeleniumWeb驱动程序TestNg中的子窗口?
在这段代码中,我使用了单类和函数类,这里AllpagesLogins它是一个类名,这段代码在函数类中编写,整个Scripit在TestNg类中运行。所以当我点击linkBtn名称时matrimonials@shaadi.com 它显示子窗口中的子窗口关闭链接按钮在那里,所以我写的子窗口代码也直接代码,但没有人工作不正常?我如何处理这个子窗口Selenium webdriver 如何使用框架处理SeleniumWeb驱动程序TestNg中的子窗口?,selenium-webdriver,Selenium Webdriver,在这段代码中,我使用了单类和函数类,这里AllpagesLogins它是一个类名,这段代码在函数类中编写,整个Scripit在TestNg类中运行。所以当我点击linkBtn名称时matrimonials@shaadi.com 它显示子窗口中的子窗口关闭链接按钮在那里,所以我写的子窗口代码也直接代码,但没有人工作不正常?我如何处理这个子窗口 package EmailMessage; import java.util.Set; import org.junit.Assert; import
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");
}
}
}
打包邮件;
导入java.util.Set;
导入org.junit.Assert;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入Class.AllpagesLogins;
//声明类名
公共类电子邮件
{
public void EMSG(WebDriver驱动程序)引发异常
{
//这里AllpagesLogins是一个类名
AllpagesLogins APL=新建AllpagesLogins();
//单击电子邮件菜单按钮
driver.findElement(By.id(APL.EMSG_EmailMsg_MenuBtn_id))。单击();
System.out.println(“单击电子邮件菜单按钮”);
《睡眠》(2000年);
//检查电子邮件标题的文本
WebElement asserta=driver.findElement(By.id(APL.EMSG_EmailMsg_mainttitle_id));
字符串a=asserta.getText();
尝试
{
Assert.assertEquals(“电子邮件”,a);
System.out.println(“作为电子邮件的主标题名称”);
}
捕获(错误e1)
{
System.out.println(“错误标题”);
}
//检查子标签的文本
WebElement assertb=driver.findElement(By.id(APL.EMSG_EmailMsg_subbl_id));
字符串b=assertb.getText();
尝试
{
Assert.assertEquals(“单击发件人电子邮件查看其详细信息。”,b);
System.out.println(“作为子标签的主标题名称”);
}
捕获(错误e1)
{
System.out.println(“错误标题”);
}
//单击子窗口
//获取父Id
字符串Currenthandle=driver.getWindowHandle();
System.out.println(“父窗口id:+Currenthandle”);
//处理子窗口
Set handles=driver.getWindowHandles();
手柄。移除(当前手柄);
//对子窗口执行操作
driver.switchTo().window(handles.iterator().next());
睡眠(3000);
//单击电子邮件菜单按钮
如果(驱动程序findElement)(按.linkText(“matrimonials@shaadi.com“”)。isDisplayed())
{
System.out.println(“正在显示链接名称”);
driver.findElement(By.linkText(“matrimonials@shaadi.com),单击();
System.out.println(“单击电子邮件Id链接按钮”);
《睡眠》(2000年);
}
其他的
{
System.out.println(“链接名称未显示”);
}
//把窗户递给孩子
if(driver.findElement(By.name(“消息详细信息”)).isDisplayed())
{
System.out.println(“显示消息详细信息标签”);
driver.findElement(By.id(APL.EMSG_ChildWindow_Close_LnkBtn_id))。单击();
System.out.println(“单击子窗口中的关闭按钮”);
《睡眠》(2000年);
}
其他的
{
System.out.println(“未显示消息详细信息标签”);
}
}
}
Webdriver childDriver=Utility.getHandleToWindow(“titleOfChildWindow”);
childDriver.close() //点击按钮后。使用此代码切换到窗口
设置winids=driver.getWindowHandles();
字符串mainWindow=winids.iterator().next();
字符串childWindow=winids.iterator().next();
driver.switchTo().window(childWindow)//这将切换到子窗口
//切换后,执行要在子窗口上执行的操作
我不明白对不起。你说的是弹出窗口吗?我说的是子窗口。
//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