public static void FindAndClickOnElement(AppiumDriver <AndroidElement> driver, string locator) { driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2); int pressX = driver.Manage().Window.Size.Width / 3; int pressy = driver.Manage().Window.Size.Height / 2; int movex = pressX - driver.Manage().Window.Size.Width / 4; int movey = pressX - driver.Manage().Window.Size.Width / 4; int i = 0; bool isPresent; do { isPresent = driver.FindElementsByXPath($"//*[contains(@text,'{locator}')]").Count > 0; if (isPresent) { AndroidElement element = driver.FindElementByXPath($"//*[contains(@text,'{locator}')]"); element.Click(); break; } else { TouchAction touchAction = new TouchAction(driver); try { touchAction.LongPress(pressX, pressy).MoveTo(movex, movey).Release().Perform(); } catch (WebDriverException wd) { Console.WriteLine(wd); } } i++; } while (i <= 4); }
/// <summary> /// 滑动屏幕封装 /// </summary> /// <param name="direction"> 滑动方向 </param> /// <param name="logOutput"> 是否打印执行日志 </param> private void BaseSwipe(string direction, bool logOutput) { try { TouchAction TouchAction = new TouchAction(AndroidDriver); switch (direction) { case "上": TouchAction.LongPress(DeviceWindowSize.Width / 2, DeviceWindowSize.Height / 2) .MoveTo(DeviceWindowSize.Width / 2, DeviceWindowSize.Height - 50).Release().Perform(); break; case "下": TouchAction.LongPress(DeviceWindowSize.Width / 2, DeviceWindowSize.Height / 2) .MoveTo(DeviceWindowSize.Width / 2, 50).Release().Perform(); break; case "左": TouchAction.LongPress(50, DeviceWindowSize.Height / 2) .MoveTo(DeviceWindowSize.Width - 50, DeviceWindowSize.Height / 2).Release().Perform(); break; case "右": TouchAction.LongPress(DeviceWindowSize.Width - 50, DeviceWindowSize.Height / 2) .MoveTo(50, DeviceWindowSize.Height / 2).Release().Perform(); break; } Thread.Sleep(500); if (logOutput) { LogServe.Info($"滑动屏幕 => {direction}"); } } catch (Exception err) { LogServe.Error($"Error:: 滑动屏幕异常: { err.Message }"); throw; } }
//[Test, TestCaseSource(nameof(TestData))] public void SearchAndMoveSeekBar(string seekBarId, double percentToMove) { GoToRotatingButton(); if (percentToMove > 1) { throw new Exception("percentToMove variable can be set only in [0,1] range"); } IWebElement seekBar = driver.FindElementById(seekBarId); driver.FindElementById(seekBarId).Tap(1, 100); //Get start point of seekbar. double startX = seekBar.Location.X; double yPos = seekBar.Location.Y; double bound = seekBar.Size.Width; double moveByX = startX + bound * percentToMove; //Moving seekbar using TouchAction class. TouchAction act = new TouchAction(driver); act.LongPress(startX, yPos).MoveTo(startX - bound / 2, yPos).Release().Perform(); startX = seekBar.Location.X; act.LongPress(startX, yPos).MoveTo(moveByX, yPos).Release().Perform(); }
public void longpress(string locator, string locator_type) { IWebElement Control = null; if (locator_type == "id") { Control = waitDriver.Until(ExpectedConditions.ElementIsVisible(By.Id(locator))); } else if (locator_type == "xpath") { Control = waitDriver.Until(ExpectedConditions.ElementIsVisible(By.XPath(locator))); } ITouchAction tc = new TouchAction(driver); tc.LongPress(Control).Perform(); }
public void CheckAppRunning() { _driver.PressKeyCode(AndroidKeyCode.Home); Thread.Sleep(1000); var clickableCountBefore = _driver.FindElementsByAndroidUIAutomator("new UiSelector().clickable(true)").Count; var pressAndHold = new TouchAction(_driver); var action = pressAndHold.LongPress(500, 700); //SUPER DIRTY HACK var stepsField = pressAndHold.GetType() .GetField("steps", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var field = (stepsField.GetValue(pressAndHold) as IEnumerable <object>).ElementAt(0); var dic = field.GetType() .GetField("parameters", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) .GetValue(field) as IDictionary; dic["action"] = "longPress"; action.Perform(); var widgetsButton = _driver.FindElementByAndroidUIAutomator("new UiSelector().textContains(\"WIDGETS\")"); widgetsButton.Click(); var kateSmall = _driver.FindElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(" + "new UiSelector().textContains(\"Kate Mobile 1x1\"));"); var pressAndHoldKateWidget = new TouchAction(_driver) .Press(kateSmall.Location.X + 10, kateSmall.Location.Y + 20).Wait(500).MoveTo(100, -200) .Wait(2000).Release(); pressAndHoldKateWidget.Perform(); var clickableCountAfter = _driver.FindElementsByAndroidUIAutomator("new UiSelector().clickable(true)").Count; Assert.True(clickableCountAfter > clickableCountBefore); }
public UiObject longClick() { IWebElement element; if (isXpath()) { element = Android.driver.FindElementByXPath(locator); } else { element = Android.driver.FindElementByAndroidUIAutomator(locator); } TouchAction action = new TouchAction(Android.driver); action.LongPress(element).Release().Perform(); return(this); }
public static void LongPress(this IWebElement element) { TouchAction touchAction = new TouchAction(mobileDriver); touchAction.LongPress(element).Perform(); }
public static void PressAndHoldElement(IWebElement webElement) { TouchAction action = new TouchAction(AppiumDriver.Instance); action.LongPress(webElement).Perform(); }