/// <summary> /// This method will select "Great Western Pilsner" from the beverage select, /// and send you to the status screen. /// </summary> private void SelectBeverage() { TouchAction touchAction = new TouchAction(driver); Thread.Sleep(500); //AndroidElement element = driver.FindElementByAndroidUIAutomator("new UiSelector().textContains(\"Please enter a beverage, type or brand!!\");"); AndroidElement element = driver.FindElementByClassName("android.widget.EditText"); element.SendKeys("Great"); Thread.Sleep(500); //element = driver.FindElementByAndroidUIAutomator("new UiSelector().textContains(\"Great Western Pilsner\");"); element = driver.FindElementByXPath("//android.widget.TextView[@text='Great Western Pilsner']"); Thread.Sleep(500); var action = touchAction.Tap(element); action.Perform(); action.Cancel(); Thread.Sleep(500); }
public void FindElementByByClassName(string element) { var duration = new Stopwatch(); var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(TestConstants.Wait)); try { Thread.Sleep(TestConstants.PageLoadTime); duration.Start(); var result = wait.Until(d => _driver.FindElementByClassName(element)); if (result != null && result.Enabled) { result.Click(); Assert.IsNotNull(result); } else { Log.Error(TestConstants.ElementNotFound + element); } duration.Stop(); Log.Info(MethodBase.GetCurrentMethod().Name + "(" + element + ")" + TestConstants.Sp + duration.ElapsedMilliseconds + TestConstants.Ms); } catch (Exception e) { Log.Error(TestConstants.ElementNotFound + element + e); throw new ElementNotVisibleException(TestConstants.ElementNotFound + element + e); } }
// Get element from selector public static AndroidElement GetElement(AndroidDriver <AndroidElement> driver, SelectBy by, string selector) { AndroidElement e = null; switch (by) { case SelectBy.ID: e = driver.FindElementById(selector); break; case SelectBy.Class: e = driver.FindElementByClassName(selector); break; case SelectBy.XPath: e = driver.FindElementByXPath(selector); break; case SelectBy.ExactText: e = driver.FindElementByAndroidUIAutomator(String.Format("new UiSelector().text(\"{0}\")", selector)); break; case SelectBy.ContainsText: e = driver.FindElementByAndroidUIAutomator(String.Format("new UiSelector().textContains(\"{0}\")", selector)); break; case SelectBy.RegexText: e = driver.FindElementByAndroidUIAutomator(String.Format("new UiSelector().textMatches(\"{0}\")", selector)); break; } return(e ?? throw new NotFoundException("Element with selector " + selector + " not found.")); }
public static void ClearTextElement(AndroidDriver <AppiumWebElement> webDriver, string by, string elementName, int timeInSeconds) { switch (by) { case "id": webDriver.FindElementById(elementName).Clear(); break; case "xPath": webDriver.FindElementByXPath(elementName).Clear(); break; case "cssSelector": webDriver.FindElementByCssSelector(elementName).Clear(); break; case "name": webDriver.FindElementByName(elementName).Clear(); break; case "className": webDriver.FindElementByClassName(elementName).Clear(); break; } Thread.Sleep(timeInSeconds * 1000); }
public static string GetValueFromElement(AndroidDriver <AppiumWebElement> webDriver, string by, string elementName, int timeInseconds) { string elementText = ""; switch (by) { case "id": elementText = webDriver.FindElementById(elementName).GetAttribute("value"); break; case "xPath": elementText = webDriver.FindElementByXPath(elementName).GetAttribute("value"); break; case "cssSelector": elementText = webDriver.FindElementByCssSelector(elementName).GetAttribute("value"); break; case "name": elementText = webDriver.FindElementByName(elementName).GetAttribute("value"); break; case "className": elementText = webDriver.FindElementByClassName(elementName).GetAttribute("value"); break; case "linkText": elementText = webDriver.FindElementByLinkText(elementName).GetAttribute("value"); break; } Thread.Sleep(timeInseconds * 1000); return(elementText); }
public void APIDemo() { AppiumDriver <AppiumWebElement> driver; var cap = new AppiumOptions(); cap.AddAdditionalCapability("deviceName", "Appium_emulator"); cap.AddAdditionalCapability("platformVersion", "9.0.0"); cap.AddAdditionalCapability("platformName", "Android"); cap.AddAdditionalCapability("appPackage", "io.appium.android.apis"); cap.AddAdditionalCapability("appActivity", "io.appium.android.apis.ApiDemos"); driver = new AndroidDriver <AppiumWebElement>(new Uri("http://127.0.0.1:4723/wd/hub"), cap); WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); //click Preference driver.FindElementByXPath("//android.widget.TextView[@content-desc='Preference']").Click(); //click PreferenceDependencies wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("//android.widget.TextView[@content-desc='3. Preference dependencies']"))).Click(); //click wifi checkbox wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("android:id/checkbox"))).Click(); //click WIFI Settings wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("(//android.widget.RelativeLayout)[2]"))).Click(); //send keys driver.FindElementByClassName("android.widget.EditText").SendKeys("hello"); //use findelements if the xpath is same for many elements and choose the index and click the element //click ok button which is in second position with the index 1 driver.FindElementsByClassName("android.widget.Button")[1].Click(); }
public void LocatingElementsTest() { AndroidElement button = _driver.FindElementById("button"); button.Click(); AndroidElement checkBox = _driver.FindElementByClassName("android.widget.CheckBox"); checkBox.Click(); AndroidElement secondButton = _driver.FindElementByAndroidUIAutomator("new UiSelector().textContains(\"BUTTO\");"); secondButton.Click(); AndroidElement thirdButton = _driver.FindElementByXPath("//*[@resource-id='com.example.android.apis:id/button']"); thirdButton.Click(); }
public void EnterAs(string name) { if (name != null) { AndroidElement nameTextbox = driver.FindElementById(NameEdittextId); nameTextbox.SendKeys(name); } else { name = "Guest"; } AndroidElement nextScreenButton = driver.FindElementByClassName(NextScreenButtonClass); nextScreenButton.Click(); AndroidElement welcomeTextview = driver.FindElementById(WelcomeTextviewId); Assert.That(welcomeTextview.Text, Is.EqualTo("Welcome " + name), "Incorrect welcome text"); }
public void TestMethod1() { AppiumDriver <AndroidElement> driver; DesiredCapabilities cap = new DesiredCapabilities(); cap.SetCapability("deviceName", "donatello"); cap.SetCapability(AndroidMobileCapabilityType.AppPackage, "com.android.calculator2"); cap.SetCapability(AndroidMobileCapabilityType.AppActivity, "Calculator"); driver = new AndroidDriver <AndroidElement>(new Uri("http://127.0.0.1:4723/wd/hub"), cap); //check if we get the application loaded Assert.IsNotNull(driver.Context); //do the UI operation driver.FindElementById("digit5").Click(); driver.FindElementById("plus").Click(); driver.FindElementById("digit2").Click(); driver.FindElementById("equal").Click(); Assert.AreEqual("7", driver.FindElementByClassName("android.widget.EditText").Text, "Values dint match"); driver.CloseApp(); }
public static void ScreenInjectionSiteLocation(AndroidDriver driver) { //Console.WriteLine("SPECIAL SCREEN - Injection Site Location"); //semi-randomly select a location int randomInt; int i; var random = new Random(); var vCollection = driver.FindElementsByClassName("android.widget.ToggleButton"); if (driver.FindElementsByClassName("android.widget.ToggleButton").Count() == 3) { randomInt = random.Next(0, 3); switch (randomInt) { case 0: //select RIGHT vCollection[0].Click(); //Console.WriteLine("Inejection Site Location Screen: Left arm"); break; case 1: //select LEFT vCollection[1].Click(); //Console.WriteLine("Inejection Site Location Screen: Right arm"); break; case 2: //select OTHER vCollection[2].Click(); Thread.Sleep(500); string randomInjection = orangutan.randomSentence(30); driver.FindElementByClassName("android.widget.EditText").SendKeys(randomInjection); //Console.WriteLine("Inejection Site Location Screen: Other(" + randomInjection + ")"); break; } //select NEXT driver.FindElementByName("Next").Click(); } else if (driver.FindElementsByClassName("android.widget.ToggleButton").Count() == 6) { randomInt = random.Next(0, 6); for (i = 0; i < 2; i++) { //set int to odd if randomInt is even on first select and vice versa if (i > 0 && randomInt % 2 == 0) { //Generate odd number randomInt = random.Next(0 / 2, 6 / 2) * 2 + 1; } else if (i > 0 && randomInt % 2 == 1) { //Generate even number randomInt = random.Next(0 / 2, 6 / 2) * 2; } switch (randomInt) { case 0: //select Left Arm vCollection[0].Click(); //Console.WriteLine("Inejection Site Location Screen: Left arm"); break; case 1: //select Right Arm vCollection[1].Click(); //Console.WriteLine("Inejection Site Location Screen: Right arm"); break; case 2: //select Left Thigh vCollection[2].Click(); //Console.WriteLine("Inejection Site Location Screen: Left thigh"); break; case 3: //select Right Thigh vCollection[3].Click(); //Console.WriteLine("Inejection Site Location Screen: Right thigh"); break; case 4: //select Other 1 vCollection[4].Click(); string randomInjection1 = orangutan.randomSentence(30); driver.FindElementByClassName("android.widget.EditText").SendKeys(randomInjection1); //Console.WriteLine("Inejection Site Location Screen: Other(" + randomInjection1 + ")"); break; case 5: //select Other 2 vCollection[5].Click(); Thread.Sleep(500); string randomInjection2 = orangutan.randomSentence(30); //Check the number of text boxes on the screen - if greater than 1 select the second and populate with random string if (driver.FindElementsByClassName("android.widget.EditText").Count() > 1) { var vColl = driver.FindElementsByClassName("android.widget.EditText"); vColl[1].SendKeys(randomInjection2); } //if only one exists populate with random string else { driver.FindElementByClassName("android.widget.EditText").SendKeys(randomInjection2); } //Console.WriteLine("Inejection Site Location Screen: Other(" + randomInjection2 + ")"); break; } } //select NEXT driver.FindElementByName("Next").Click(); } else { //Console.WriteLine("FAIL @ Injection Site Location Screen: Missmatch in Collection Count: Count = " + driver.FindElementsByClassName("android.widget.ToggleButton").Count().ToString()); } }
public static string enrolSubject(string loginPIN, string subjectID, string centreID, AndroidDriver driver) { string sTempPIN = "false"; //login as site admin //standroid.GetScreenShot(driver, "test"); //standroid.takeScreenshot(driver); driver.FindElementByClassName("android.widget.EditText").SendKeys(loginPIN); driver.FindElementByName("Next").Click(); //select New Subject GenericFunctions.Wait(5); driver.FindElementByName("New Subject").Click(); //enter new subject enrollment details GenericFunctions.Wait(1); //standroid.GetScreenShot(driver, "screenshot"); orangutan.CaptureElementCollection("android.widget.EditText", driver); var fields = driver.FindElementsByClassName("android.widget.EditText"); fields[0].SendKeys(centreID); fields[1].SendKeys(centreID); fields[2].SendKeys(subjectID); fields[3].SendKeys(subjectID); //flick screen to get next question in view var touch = new RemoteTouchScreen(driver); GenericFunctions.Wait(1); touch.Flick(500, -1); GenericFunctions.Wait(1); touch.Flick(500, -1); GenericFunctions.Wait(1); //select 'No' to replacement phone question driver.FindElementByName("No").Click(); //select Next to confirm enrolment form details driver.FindElementByName("Next").Click(); GenericFunctions.Wait(1); //Capture the temporary PIN and SUBMIT orangutan.CaptureElementCollection("android.widget.TextView", driver); fields = driver.FindElementsByClassName("android.widget.TextView"); sTempPIN = fields[6].Text; //check it captured it correctly if (sTempPIN.Length != 4) { //didn't capture it correctly - return false sTempPIN = "false"; } //Console.WriteLine("sTempPIN = " + sTempPIN); driver.FindElementByName("Submit").Click(); //Select Next in the enrolment confirmation screen Thread.Sleep(1000); //standroid.GetScreenShot(driver, "screenshot"); driver.FindElementByName("Next").Click(); //Logout of the site admin side Thread.Sleep(1000); //standroid.GetScreenShot(driver, "screenshot"); driver.FindElementByName("Logout").Click(); //standroid.GetScreenShot(driver, "screenshot"); return(sTempPIN); }
public void AppiumTestMethod() { //Write your test here try { // write your code here string title = "Test Scripting"; IWebElement field; try { field = driver.FindElementByXPath("//android.widget.TextView[@text='GET STARTED']"); field.Click(); Trace.WriteLine("Pressed the Get Started button to get into the application"); } catch (NoSuchElementException n) { Trace.WriteLine("No Welcome screen - just plow ahead"); } field = driver.FindElementByXPath("//*[@resource-id='com.google.android.keep:id/new_list_button']"); field.Click();; // give the list a title - that can be checked on the main Note-board display field = driver.FindElementByXPath("//*[@resource-id='com.google.android.keep:id/editable_title']"); field.Click(); field.SendKeys(title); // app is now ready to accept text for the first list item field = driver.FindElementByXPath("//android.widget.EditText[@resource-id='com.google.android.keep:id/description' and @text ='']"); field.Click(); field.SendKeys("Select app to test"); // click the add field field = driver.FindElementByXPath("//android.widget.ImageView[parent::android.widget.LinearLayout[@content-desc='Add list item']]"); field.Click(); // Enter the second line in the list field = driver.FindElementByXPath("//android.widget.EditText[@resource-id='com.google.android.keep:id/description' and @text ='']"); field.SendKeys("Write the script"); // click the add field field = driver.FindElementByXPath("//android.widget.ImageView[parent::android.widget.LinearLayout[@content-desc='Add list item']]"); field.Click(); // return to the Note-board field = driver.FindElementByClassName("android.widget.ImageButton"); field.Click(); try { //Verify that new note appears on the Note-board field = driver.FindElementByXPath("//android.widget.TextView[@resource-id='com.google.android.keep:id/title' and @text='" + title + "']"); } catch (NoSuchElementException f) { Trace.WriteLine("Did not find the note on the NoteBoard"); } // cleanup the application before closing the connection String cmnd = "mobile:application:clean"; Dictionary <String, Object> pars = new Dictionary <String, Object>(); pars.Add("name", "keep"); driver.ExecuteScript(cmnd, pars); } catch (Exception e) { Trace.WriteLine("Script failed with the exception:" + e.Message); } }
// TODO: May need a platform switch public static AndroidElement GetAlert(this AndroidDriver <AndroidElement> driver) { return(driver.FindElementByClassName("androidx.appcompat.widget.AppCompatTextView")); }
public void AppiumTestMethod() { //Write your test here try { // write your code here By newList = By.XPath("//*[@resource-id='com.google.android.keep:id/new_list_button']"); By titleF = By.XPath("//*[@resource-id='com.google.android.keep:id/editable_title']"); By newItem = By.XPath("//*[@resource-id='com.google.android.keep:id/add_item_text_view']"); By addItem = By.XPath("//*[@resource-id='com.google.android.keep:id/add_item_extra_text_view']"); By descLine = By.XPath("//*[@resource-id='com.google.android.keep:id/description' and @focusable='true']"); IWebElement field; //first point of interest WindTunnelUtils.PointOfInterest(driver, "Adding the list", PointOfInterestStatus.Success); String title = "Test Scripting"; field = driver.FindElement(newList); field.Click();; // give the list a title - that can be checked on the main Note-board display field = driver.FindElement(titleF); field.SendKeys(title); // app is now ready to accept text for the first list item field = driver.FindElement(newItem); field.Click(); field.SendKeys("Select app to test"); // click the add field field = driver.FindElement(addItem); field.Click(); // Enter the second line in the list field = driver.FindElement(newItem); field.SendKeys("Write the script"); // click the add field field = driver.FindElement(addItem); field.Click(); // return to the Note-board field = driver.FindElementByClassName("android.widget.ImageButton"); field.Click(); // Second POI - completed the list. WindTunnelUtils.PointOfInterest(driver, "Completed entry", PointOfInterestStatus.Success); try { //Verify that new note appears on the Note-board field = driver.FindElementByXPath("//*[@resource-id=\"com.google.android.keep:id/title\" and @text=\"" + title + "\"]"); WindTunnelUtils.PointOfInterest(driver, "Found list on NoteBoard", PointOfInterestStatus.Success); } catch (NoSuchElementException f) { Trace.WriteLine("Did not find the note on the NoteBoard"); WindTunnelUtils.PointOfInterest(driver, "Failed to find list on NoteBoard", PointOfInterestStatus.Failure); } // cleanup the application before closing the connection String cmnd = "mobile:application:clean"; Dictionary <String, Object> pars = new Dictionary <String, Object>(); pars.Add("name", "keep"); driver.ExecuteScript(cmnd, pars); } catch (Exception e) { Trace.WriteLine("Script failed with the exception:" + e.Message); } }