我试图在移动设备中自动化一个不是响应式网站的网站。只有当我使用chrome选项中的菜单手动将网站更改为桌面视图模式时,才会面临挑战。是否有任何设置或代码可用于自动执行此操作。我想默认将网站设为桌面视图。 有趣的RemoteDriver
[TestMethod]
public void TestMethod1()
{
DesiredCapabilities caps = new DesiredCapabilities();
caps.SetCapability(MobileCapabilityType.BrowserName,"Chrome");
caps.SetCapability(MobileCapabilityType.PlatformName, "Android");
caps.SetCapability(MobileCapabilityType.PlatformVersion, "7.1");
caps.SetCapability(MobileCapabilityType.DeviceName, "ZL33B2YDT5");
caps.SetCapability("unicodeKeyboard", true);
caps.SetCapability("resetKeyboard", true);
dr = new RemoteWebDriver(new Uri("http://127.0.0.1:4723/wd/hub"), caps);
dr.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromMinutes(5);
dr.Manage().Timeouts().ImplicitWait = TimeSpan.FromMinutes(10);
dr.Manage().Cookies.DeleteAllCookies();
dr.Navigate().GoToUrl(URL);
dr.FindElement(By.Id("txtUsername")).SendKeys("su");
dr.FindElement(By.Id("txtPassword")).SendKeys("sudemo");
IWebElement login = dr.FindElement(By.Id("btn_SignIn"));
Assert.IsTrue(login.Displayed);
login.Click();
List<string> lstWindow = dr.WindowHandles.ToList();
String lastWindowHandle = "";
foreach (var handle in lstWindow)
{
Thread.Sleep(1000);
lastWindowHandle = handle;
Thread.Sleep(1000);
}
Thread.Sleep(2000);
dr.Close();
dr.SwitchTo().Window(lastWindowHandle);
//To mouse hover on gen element the site has to be in Desktop view. If its in normal view hover is not happening but if i make site to Desktop View manually hover happening without issue
string javaScript = "var evObj = document.createEvent('MouseEvents');" +
"evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
"arguments[0].dispatchEvent(evObj);";
IJavaScriptExecutor js = dr as IJavaScriptExecutor;
IWebElement gen = dr.FindElement(By.XPath("//ul[@class='rmRootGroup rmRoundedCorners rmShadows rmHorizontal']/li[2]/a //span[contains(text(),'General')]"));
js.ExecuteScript(javaScript, gen);
IWebElement group = dr.FindElement(By.XPath("//ul[@class='rmRootGroup rmRoundedCorners rmShadows rmHorizontal']/li[2] //ul[@class='rmVertical']/li/a //span[contains(text(),'Group')]"));
group.Click();
dr.FindElement(By.LinkText("Logout")).Click();
Thread.Sleep(5000);
dr.Quit();
}
提前感谢您等待回复