从Browser2(Chrome)切换后,我无法访问Browser1(IE)上的控件
场景是:
我在下面写了代码片段。
public static BrowserWindow CurrentBrowser = null, Chrome = null, IE = null;
[TestMethod]
public void CodedUITestMethod1()
{
IE = BrowserWindow.Launch(new Uri("http://login.live.com"));
BrowserWindow.CurrentBrowser = "Chrome";
Chrome = BrowserWindow.Launch(new Uri("http://login.live.com"));
CurrentBrowser = GetMeTheBrowserIWant("Chrome");
HtmlControl LogOnButton = new HtmlControl(CurrentBrowser);
LogOnButton.SearchProperties.Add(HtmlControl.PropertyNames.Id, "idSIButton9");
Mouse.Click(LogOnButton);
CurrentBrowser = GetMeTheBrowserIWant("IE");
CurrentBrowser.SetFocus();
Mouse.Click(LogOnButton); // Here, it clicks the Sign in button on Chrome. Not on IE
}
public static BrowserWindow GetMeTheBrowserIWant(string BrowserType)
{
if(BrowserType.ToUpper().Equals("CHROME"))
{
return Chrome;
}
if(BrowserType.ToUpper().Equals("IE"))
{
return IE;
}
return null;
}
但是在最后一步,即使我已将CurrentBrowser设置为“IE”,它也会点击Chrome浏览器上的“登录”按钮。
如何点击“IE”浏览器上的“登录”?
提前致谢
答案 0 :(得分:0)
您的测试代码中存在缺陷,您的搜索限制容器尚未更新
[TestMethod]
public void CodedUITestMethod1()
{
IE = BrowserWindow.Launch(new Uri("http://login.live.com"));
BrowserWindow.CurrentBrowser = "Chrome";
Chrome = BrowserWindow.Launch(new Uri("http://login.live.com"));
CurrentBrowser = GetMeTheBrowserIWant("Chrome");
此时,您当前浏览器的定义是镀铬窗口
HtmlControl LogOnButton = new HtmlControl(CurrentBrowser);
LogOnButton.SearchProperties.Add(HtmlControl.PropertyNames.Id, "idSIButton9");
Mouse.Click(LogOnButton);
您可能会覆盖该字段,但Chrome浏览器仍在引用Chrome窗口
CurrentBrowser = GetMeTheBrowserIWant("IE");
CurrentBrowser.SetFocus();
现在正如目前所写,这实际上是按预期工作 - >单击登录按钮 IN chrome,因为您在chrome窗口中定义了它;
Mouse.Click(LogOnButton);
}
您可以再次分配其容器或创建新的登录按钮。
LogonButton.Container = CurrentBrowser;
// or
var buttonInIE = new HtmlControl(IEBW);
var buttonInChrome = new HtmlControl(ChromeBW);
记住Codedui基于控件中的控件
答案 1 :(得分:0)
如果流程如上所示 - 打开IE,打开Chrome,登录Chrome并登录IE,您可以使用以下内容找到您的IE浏览器
BrowserWindow.Locate(" SiteTitle") - 这将按定义的标题定位浏览器窗口。请注意,“定位”仅适用于IE而不适用于Chrome。
找到IE浏览器并单击“登录”后,“登录”操作将在IE浏览器上进行。
您的代码应为:
public class IEAndChrome
{
private BrowserWindow currentBrowser = null;
private BrowserWindow chrome = null;
private BrowserWindow iE = null;
public BrowserWindow Browser { get; set; }
[TestMethod]
public void CodedUITestMethod1()
{
iE = BrowserWindow.Launch(new Uri("http://login.live.com"));
BrowserWindow.CurrentBrowser = "Chrome";
chrome = BrowserWindow.Launch(new Uri("http://login.live.com"));
currentBrowser = GetMeTheBrowserIWant("Chrome");
HtmlControl logOnButton = new HtmlControl(currentBrowser);
logOnButton.SearchProperties.Add(HtmlControl.PropertyNames.Id, "idSIButton9");
Mouse.Click(logOnButton);
currentBrowser = BrowserWindow.Locate("Sign in to your Microsoft account");
Mouse.Click(logOnButton); // Here, it clicks the Sign in button on Chrome. Not on IE
}
这应该适合你,验证它