public void ShouldAdd() { StringAssert.AreEqualIgnoringCase(ngDriver.Title, "Super Calculator"); var ng_first_operand = ngDriver.FindElement(NgBy.Model("first")); ng_first_operand.SendKeys("1"); #pragma warning disable 618 NgWebElement ng_second_operand = ngDriver.FindElement(NgBy.Input("second")); #pragma warning restore 618 ng_second_operand.SendKeys("2"); NgWebElement ng_math_operator_element = ngDriver.FindElement(NgBy.Options("value for (key, value) in operators")); Assert.AreEqual(ng_math_operator_element.Text, "+"); IWebElement math_operator_element = ngDriver.FindElement(NgBy.SelectedOption("operator")); Assert.AreEqual(math_operator_element.Text, "+"); IWebElement go_button_element = ngDriver.FindElement(NgBy.PartialButtonText("Go")); Assert.IsTrue(go_button_element.Displayed); var ng_go_button_element = ngDriver.FindElement(By.Id("gobutton")); ng_go_button_element.Click(); string ng_go_button_element_css_selector = ng_go_button_element.CssSelectorOf(); string ng_go_button_element_xpath = ng_go_button_element.XPathOf(); NgWebElement result_element = ngDriver.FindElement(NgBy.Binding("latest")); Assert.AreEqual("3", result_element.Text); ngDriver.Highlight(result_element, 1000); }
public void ShouldSearchCitySuggestions() { String city = "Jacksonville, FL"; wait.Until(ExpectedConditions.ElementIsVisible(NgBy.Model("term"))); IWebElement search = driver.FindElement(By.XPath("//input[@name='search']")); Assert.IsNotNull(search); ngDriver.Highlight(search); // NOTE: Chrome is occasionally dropping first letter . // search.SendKeys(city[0].ToString()); // TODO: http://stackoverflow.com/questions/1450774/splitting-a-string-into-chunks-of-a-certain-size foreach (char cityChar in city.ToCharArray()) { Console.Error.WriteLine("Sending: {0}", cityChar); search.SendKeys(cityChar.ToString()); Thread.Sleep(50); } search.Click(); ReadOnlyCollection <NgWebElement> ng_elements = ngDriver.FindElements(NgBy.Repeater("item in results | limitTo:10")); foreach (NgWebElement ng_element in ng_elements) { try { Assert.IsNotNull(ng_element.FindElement(NgBy.Binding("getPresName($index)"))); Console.Error.WriteLine("Suggested: {0}", ng_element.Text); } catch (StaleElementReferenceException e) { Console.Error.WriteLine("Ignored exception: {0}", e.Message); } } NgWebElement ng_firstMatchingElement = ng_elements.First(x => x.Text.ToLower() == city.ToLower()); Assert.IsNotNull(ng_firstMatchingElement); ngDriver.Highlight(ng_firstMatchingElement); Console.Error.WriteLine("Clicking: {0}", ng_firstMatchingElement.Text); ng_firstMatchingElement.Click(); Thread.Sleep(1000); // TODO: Assert the change of the URL }
public void ShouldAddFriend() { StringAssert.AreEqualIgnoringCase(ngDriver.Title, "Angular JS Demo"); String friendName = "John Doe"; int friendCount = ngDriver.FindElements(NgBy.Repeater("row in rows")).Count; NgWebElement addnameBox = ngDriver.FindElement(NgBy.Model("addName")); Assert.IsNotNull(addnameBox); ngDriver.Highlight(addnameBox, highlight_timeout); addnameBox.SendKeys(friendName); // add the friend NgWebElement addButton = ngDriver.FindElement(NgBy.ButtonText("+ add")); Assert.IsNotNull(addButton); ngDriver.Highlight(addButton, highlight_timeout); addButton.Click(); // confirm the number of friends Assert.AreEqual(1, ngDriver.FindElements(NgBy.Repeater("row in rows")).Count - friendCount); // find friend NgWebElement addedFriendElement = ngDriver.FindElements(NgBy.CssContainingText("td.ng-binding", friendName)).First(); Assert.IsNotNull(addedFriendElement); ngDriver.Highlight(addedFriendElement, highlight_timeout); Console.Error.WriteLine("Added friend name: " + addedFriendElement.Text); }
public void ShouldDeposit() { ngDriver.FindElement(NgBy.ButtonText("Customer Login")).Click(); ReadOnlyCollection <NgWebElement> ng_customers = ngDriver.FindElement(NgBy.Model("custId")).FindElements(NgBy.Repeater("cust in Customers")); // select customer to log in ng_customers.First(cust => Regex.IsMatch(cust.Text, "Harry Potter")).Click(); ngDriver.FindElement(NgBy.ButtonText("Login")).Click(); ngDriver.FindElement(NgBy.Options("account for account in Accounts")).Click(); // inspect the account NgWebElement ng_account_number = ngDriver.FindElement(NgBy.Binding("accountNo")); int account_id = 0; int.TryParse(ng_account_number.Text.FindMatch(@"(?<account_number>\d+)$"), out account_id); Assert.AreNotEqual(0, account_id); /* * IEnumerable<IWebElement>elements = driver.FindElements(By.CssSelector("[data-id]")); * int[] results = elements.TakeWhile(e => Regex.IsMatch(e.GetAttribute("data-id") , "[0-9]+" )).Select(x => Int32.Parse(x.GetAttribute("data-id"))).ToArray<int>(); */ int account_balance = -1; int.TryParse(ngDriver.FindElement(NgBy.Binding("amount")).Text.FindMatch(@"(?<account_balance>\d+)$"), out account_balance); Assert.AreNotEqual(-1, account_balance); NgWebElement ng_deposit_button = ngDriver.FindElement(NgBy.PartialButtonText("Deposit")); Assert.IsTrue(ng_deposit_button.Displayed); actions.MoveToElement(ng_deposit_button.WrappedElement).Build().Perform(); Thread.Sleep(500); ng_deposit_button.Click(); // core Selenium wait.Until(ExpectedConditions.ElementExists(By.CssSelector("form[name='myForm']"))); NgWebElement ng_form_element = new NgWebElement(ngDriver, driver.FindElement(By.CssSelector("form[name='myForm']"))); // deposit amount NgWebElement ng_deposit_amount = ng_form_element.FindElement(NgBy.Model("amount")); ng_deposit_amount.SendKeys("100"); // Confirm to perform deposit NgWebElement ng_submit_deposit_button = ng_form_element.FindElements(NgBy.ButtonText("Deposit")).First(o => o.GetAttribute("class").IndexOf("btn-default", StringComparison.InvariantCultureIgnoreCase) > -1); actions.MoveToElement(ng_submit_deposit_button.WrappedElement).Build().Perform(); ngDriver.Highlight(ng_submit_deposit_button, highlight_timeout); ng_submit_deposit_button.Click(); // http://www.way2automation.com/angularjs-protractor/banking/depositTx.html // inspect message var ng_message = ngDriver.FindElement(NgBy.Binding("message")); StringAssert.Contains("Deposit Successful", ng_message.Text); ngDriver.Highlight(ng_message, highlight_timeout); // re-read the amount int updated_account_balance = -1; int.TryParse(ngDriver.FindElement(NgBy.Binding("amount")).Text.FindMatch(@"(?<account_balance>\d+)$"), out updated_account_balance); Assert.AreEqual(updated_account_balance, account_balance + 100); }
public void ShouldDirectSelectFromDatePicker() { Common.GetLocalHostPageContent("ng_datepicker.htm"); // http://dalelotts.github.io/angular-bootstrap-datetimepicker/ NgWebElement ng_result = ngDriver.FindElement(NgBy.Model("data.inputOnTimeSet")); ng_result.Clear(); ngDriver.Highlight(ng_result); IWebElement calendar = ngDriver.FindElement(By.CssSelector(".input-group-addon")); ngDriver.Highlight(calendar); Actions actions = new Actions(ngDriver.WrappedDriver); actions.MoveToElement(calendar).Click().Build().Perform(); int datepicker_width = 900; int datepicker_heght = 800; driver.Manage().Window.Size = new System.Drawing.Size(datepicker_width, datepicker_heght); IWebElement dropdown = driver.FindElement(By.CssSelector("div.dropdown.open ul.dropdown-menu")); NgWebElement ng_dropdown = new NgWebElement(ngDriver, dropdown); Assert.IsNotNull(ng_dropdown); ReadOnlyCollection <NgWebElement> elements = ng_dropdown.FindElements(NgBy.Repeater("dateObject in week.dates")); Assert.IsTrue(28 <= elements.Count); String monthDate = "12"; IWebElement dateElement = ng_dropdown.FindElements(NgBy.CssContainingText("td.ng-binding", monthDate)).First(); Console.Error.WriteLine("Mondh Date: " + dateElement.Text); dateElement.Click(); NgWebElement ng_element = ng_dropdown.FindElement(NgBy.Model("data.inputOnTimeSet")); Assert.IsNotNull(ng_element); ngDriver.Highlight(ng_element); ReadOnlyCollection <NgWebElement> ng_dataDates = ng_element.FindElements(NgBy.Repeater("dateObject in data.dates")); Assert.AreEqual(24, ng_dataDates.Count); String timeOfDay = "6:00 PM"; NgWebElement ng_hour = ng_element.FindElements(NgBy.CssContainingText("span.hour", timeOfDay)).First(); Assert.IsNotNull(ng_hour); ngDriver.Highlight(ng_hour); Console.Error.WriteLine("Hour of the day: " + ng_hour.Text); ng_hour.Click(); String specificMinute = "6:35 PM"; // reload // dropdown = driver.FindElement(By.CssSelector("div.dropdown.open ul.dropdown-menu")); // ng_dropdown = new NgWebElement(ngDriver, dropdown); ng_element = ng_dropdown.FindElement(NgBy.Model("data.inputOnTimeSet")); Assert.IsNotNull(ng_element); ngDriver.Highlight(ng_element); NgWebElement ng_minute = ng_element.FindElements(NgBy.CssContainingText("span.minute", specificMinute)).First(); Assert.IsNotNull(ng_minute); ngDriver.Highlight(ng_minute); Console.Error.WriteLine("Time of the day: " + ng_minute.Text); ng_minute.Click(); ng_result = ngDriver.FindElement(NgBy.Model("data.inputOnTimeSet")); ngDriver.Highlight(ng_result, 100); Console.Error.WriteLine("Selected Date/time: " + ng_result.GetAttribute("value")); }
public void ShouldSelectOneByOne() { // Given multuselect directive NgWebElement ng_directive = ngDriver.FindElement(NgBy.Model("selectedCar")); Assert.IsNotNull(ng_directive.WrappedElement); Assert.That(ng_directive.TagName, Is.EqualTo("am-multiselect")); // open am-multiselect IWebElement toggleSelect = ng_directive.FindElement(By.CssSelector("button[ng-click='toggleSelect()']")); Assert.IsNotNull(toggleSelect); Assert.IsTrue(toggleSelect.Displayed); toggleSelect.Click(); // When I want to select every "Audi", "Honda" or "Toyota" car String makeMatcher = "(?i:" + String.Join("|", new String[] { "audi", "honda", "toyota" }) + ")"; ReadOnlyCollection <NgWebElement> cars = ng_directive.FindElements(NgBy.Repeater("i in items")); Assert.Greater(cars.Count(car => Regex.IsMatch(car.Text, makeMatcher)), 0); // And I pick every matching car one item at a time int selected_cars_count = 0; for (int num_row = 0; num_row < cars.Count(); num_row++) { NgWebElement ng_item = ng_directive.FindElement(NgBy.Repeaterelement("i in items", num_row, "i.label")); if (Regex.IsMatch(ng_item.Text, makeMatcher, RegexOptions.IgnoreCase)) { Console.Error.WriteLine("Selecting: " + ng_item.Text); ng_item.Click(); selected_cars_count++; ngDriver.Highlight(ng_item, highlight_timeout); } } // Then button text shows the total number of cars I have selected IWebElement button = driver.FindElement(By.CssSelector("am-multiselect > div > button")); ngDriver.Highlight(button, highlight_timeout); StringAssert.IsMatch(@"There are (\d+) car\(s\) selected", button.Text); int displayed_count = 0; int.TryParse(button.Text.FindMatch(@"(?<count>\d+)"), out displayed_count); Assert.AreEqual(displayed_count, selected_cars_count); Console.Error.WriteLine("Button text: " + button.Text); try { // NOTE: the following does not work: // ms-selected = "There are {{selectedCar.length}} NgWebElement ng_button = new NgWebElement(ngDriver, button); Console.Error.WriteLine(ng_button.GetAttribute("innerHTML")); NgWebElement ng_length = ng_button.FindElement(NgBy.Binding("selectedCar.length")); ng_length = ngDriver.FindElement(NgBy.Binding("selectedCar.length")); Console.Error.WriteLine(ng_length.Text); } catch (NullReferenceException) { } }
public void ShouldDeposit() { ngDriver.FindElement(NgBy.ButtonText("Customer Login")).Click(); ReadOnlyCollection <NgWebElement> ng_customers = ngDriver.FindElement(NgBy.Model("custId")).FindElements(NgBy.Repeater("cust in Customers")); // select customer to log in ng_customers.First(cust => Regex.IsMatch(cust.Text, "Harry Potter")).Click(); ngDriver.FindElement(NgBy.ButtonText("Login")).Click(); ngDriver.FindElement(NgBy.Options("account for account in Accounts")).Click(); // inspect the account NgWebElement ng_account_number = ngDriver.FindElement(NgBy.Binding("accountNo")); int account_id = 0; int.TryParse(ng_account_number.Text.FindMatch(@"(?<account_number>\d+)$"), out account_id); Assert.AreNotEqual(0, account_id); int account_balance = -1; int.TryParse(ngDriver.FindElement(NgBy.Binding("amount")).Text.FindMatch(@"(?<account_balance>\d+)$"), out account_balance); Assert.AreNotEqual(-1, account_balance); NgWebElement ng_deposit_button = ngDriver.FindElement(NgBy.PartialButtonText("Deposit")); Assert.IsTrue(ng_deposit_button.Displayed); actions.MoveToElement(ng_deposit_button.WrappedElement).Build().Perform(); Thread.Sleep(500); ng_deposit_button.Click(); // core Selenium wait.Until(ExpectedConditions.ElementExists(By.CssSelector("form[name='myForm']"))); NgWebElement ng_form_element = new NgWebElement(ngDriver, driver.FindElement(By.CssSelector("form[name='myForm']"))); // deposit amount NgWebElement ng_deposit_amount = ng_form_element.FindElement(NgBy.Model("amount")); ng_deposit_amount.SendKeys("100"); wait.Until(ExpectedConditions.ElementIsVisible(NgBy.ButtonText("Deposit"))); ng_deposit_button = ng_form_element.FindElement(NgBy.ButtonText("Deposit")); actions.MoveToElement(ng_deposit_button.WrappedElement).Build().Perform(); ngDriver.Highlight(ng_deposit_button); ng_deposit_button.Click(); // http://www.way2automation.com/angularjs-protractor/banking/depositTx.html // inspect message var ng_message = ngDriver.FindElement(NgBy.Binding("message")); StringAssert.Contains("Deposit Successful", ng_message.Text); ngDriver.Highlight(ng_message); // re-read the amount int updated_account_balance = -1; int.TryParse(ngDriver.FindElement(NgBy.Binding("amount")).Text.FindMatch(@"(?<account_balance>\d+)$"), out updated_account_balance); Assert.AreEqual(updated_account_balance, account_balance + 100); }
public void ShouldHandleAngularUISelect() { Common.GetLocalHostPageContent("ng_ui_select_example1.htm"); ReadOnlyCollection <NgWebElement> ng_selected_colors = ngDriver.FindElements(NgBy.Repeater("$item in $select.selected")); Assert.IsTrue(2 == ng_selected_colors.Count); foreach (NgWebElement ng_selected_color in ng_selected_colors) { ngDriver.Highlight(ng_selected_color); Object selected_color_item = ng_selected_color.Evaluate("$item"); Console.Error.WriteLine(String.Format("selected color: {0}", selected_color_item.ToString())); } // IWebElement search = ngDriver.FindElement(By.CssSelector("input[type='search']")); // same element NgWebElement ng_search = ngDriver.FindElement(NgBy.Model("$select.search")); ng_search.Click(); int wait_seconds = 3; WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(wait_seconds)); // https://stackoverflow.com/questions/10934305/selenium-c-sharp-webdriver-how-to-detect-if-element-is-visible /* * wait.Until(d => (d.FindElements(By.CssSelector("div[role='option']"))).Count > 0); */ /* * wait.Until(d => { * IWebElement element = null; * try { * * element = d.FindElement(By.CssSelector("div[role='option']")); * return element.Displayed && element.Enabled; * } catch (NoSuchElementException exception) { * return false; * } * }); */ wait.Until(d => { IWebElement element = null; if (TryFindElement(By.CssSelector("div[role='option']"), out element)) { return(element.Displayed && element.Enabled); } else { return(false); } }); ReadOnlyCollection <NgWebElement> ng_available_colors = ngDriver.FindElements(By.CssSelector("div[role='option']")); Assert.IsTrue(6 == ng_available_colors.Count); foreach (NgWebElement ng_available_color in ng_available_colors) { ngDriver.Highlight(ng_available_color); int available_color_index = -1; try { available_color_index = Int32.Parse(ng_available_color.Evaluate("$index").ToString()); } catch (Exception) { // ignore } Console.Error.WriteLine(String.Format("available color [{1}]:{0}", ng_available_color.Text, available_color_index)); } }
public void ShouldHandleSearchAngularUISelect() { GetPageContent("ng_ui_select_example1.htm"); String searchText = "Ma"; IWebElement search = ngDriver.FindElement(By.CssSelector("input[type='search']")); search.SendKeys(searchText); NgWebElement ng_search = new NgWebElement(ngDriver, search); StringAssert.IsMatch(@"input", ng_search.TagName); // triggers ngDriver.waitForAngular(); ReadOnlyCollection <IWebElement> available_colors = ngDriver.WrappedDriver.FindElements(By.CssSelector("div[role='option']")); var matching_colors = available_colors.Where(color => color.Text.Contains(searchText)); foreach (IWebElement matching_color in matching_colors) { ngDriver.Highlight(matching_color); Console.Error.WriteLine(String.Format("Matched color: {0}", matching_color.Text)); } }
public void ShouldHighlightCurrentMonthDays() { // Arrange try { wait.Until(e => e.FindElements( By.ClassName("col-sm-6")).Any(element => element.Text.Contains("Embedded calendar"))); } catch (Exception e) { verificationErrors.Append(e.Message); } NgWebElement ng_datepicker = ngDriver.FindElement(NgBy.Model("data.embeddedDate", "*[data-ng-app]")); Assert.IsNotNull(ng_datepicker); // NOTE: cannot highlight calendar, only individual days actions.MoveToElement(ng_datepicker).Build().Perform(); ngDriver.Highlight(ng_datepicker); NgWebElement[] ng_dates = ng_datepicker.FindElements(NgBy.Repeater("dateObject in week.dates")).ToArray(); Assert.IsTrue(28 <= ng_dates.Length); // Act // Highlight every day in the month int start = 0, end = ng_dates.Length; for (int cnt = 0; cnt != ng_dates.Length; cnt++) { if (start == 0 && Convert.ToInt32(ng_dates[cnt].Text) == 1) { start = cnt; } if (cnt > start && Convert.ToInt32(ng_dates[cnt].Text) == 1) { end = cnt; } } for (int cnt = start; cnt != end; cnt++) { NgWebElement ng_date = ng_dates[cnt]; ngDriver.Highlight(ng_date, highlight_timeout, 3, (ng_date.GetAttribute("class").Contains("current")) ? "blue" : "green"); } }