public void ShouldDragAndDrop() { Common.GetLocalHostPageContent("ng_drag_and_drop1.htm"); ReadOnlyCollection <NgWebElement> ng_cars = ngDriver.FindElements(NgBy.Repeater("car in models.cars")); Assert.AreEqual(5, ng_cars.Count); foreach (NgWebElement ng_car in ng_cars) { try { ngDriver.Highlight(ng_car); actions.MoveToElement(ng_car).Build().Perform(); IWebElement basket = driver.FindElement(By.XPath("//*[@id='my-basket']")); // works in Java, desktop browser actions.ClickAndHold(ng_car).MoveToElement(basket).Release().Build() .Perform(); Thread.Sleep(1000); NgWebElement ng_basket = new NgWebElement(ngDriver, basket); ReadOnlyCollection <NgWebElement> ng_cars_basket = ng_basket.FindElements(NgBy.Repeater("car in models.basket")); NgWebElement ng_car_basket = ng_cars_basket.Last(); Assert.IsTrue(ng_car_basket.Displayed); // {{ car.name }} - {{ car.modelYear }} ( {{ car.price | currency }} ) Console.Error.WriteLine("%s - %s ( %s )", ng_car_basket.Evaluate("car.name"), ng_car_basket.Evaluate("car.modelYear"), ng_car_basket.Evaluate("car.price | currency")); } catch (Exception e) { // System.InvalidOperationException: Sequence contains no elements // TODO Console.Error.WriteLine(e.ToString()); } } }
public void ShouldHandleDeselectAngularUISelect() { Common.GetLocalHostPageContent("ng_ui_select_example1.htm"); ReadOnlyCollection <NgWebElement> ng_selected_colors = ngDriver.FindElements(NgBy.Repeater("$item in $select.selected")); while (true) { ng_selected_colors = ngDriver.FindElements(NgBy.Repeater("$item in $select.selected")); if (ng_selected_colors.Count == 0) { break; } NgWebElement ng_deselect_color = ng_selected_colors.Last(); Object itemColor = ng_deselect_color.Evaluate("$item"); Console.Error.WriteLine(String.Format("Deselecting color: {0}", itemColor.ToString())); IWebElement ng_close = ng_deselect_color.FindElement(By.CssSelector("span[class *='close']")); Assert.IsNotNull(ng_close); Assert.IsNotNull(ng_close.GetAttribute("ng-click")); StringAssert.IsMatch(@"removeChoice", ng_close.GetAttribute("ng-click")); ngDriver.Highlight(ng_close); ng_close.Click(); // ngDriver.waitForAngular(); } Console.Error.WriteLine("Nothing is selected"); }
public void ShouldUpload() { Common.GetLocalHostPageContent("ng_upload1.htm"); // NOTE: does not work with Common.GetPageContent("ng_upload1.htm"); IWebElement file = driver.FindElement(By.CssSelector("div[ng-controller = 'myCtrl'] > input[type='file']")); Assert.IsNotNull(file); StringAssert.AreEqualIgnoringCase(file.GetAttribute("file-model"), "myFile"); String localPath = Common.CreateTempFile("lorem ipsum dolor sit amet"); IAllowsFileDetection fileDetectionDriver = driver as IAllowsFileDetection; if (fileDetectionDriver == null) { Assert.Fail("driver does not support file detection. This should not be"); } fileDetectionDriver.FileDetector = new LocalFileDetector(); try { file.SendKeys(localPath); } catch (WebDriverException e) { // the operation has timed out Console.Error.WriteLine(e.Message); } NgWebElement button = ngDriver.FindElement(NgBy.ButtonText("Upload")); button.Click(); NgWebElement ng_file = new NgWebElement(ngDriver, file); Object myFile = ng_file.Evaluate("myFile"); if (myFile != null) { Dictionary <String, Object> result = (Dictionary <String, Object>)myFile; Assert.IsTrue(result.Keys.Contains("name")); Assert.IsTrue(result.Keys.Contains("type")); Assert.IsTrue(result.Keys.Contains("size")); } else { Console.Error.WriteLine("myFile is null"); } String script = "var e = angular.element(arguments[0]); var f = e.scope().myFile; if (f){return f.name} else {return null;}"; try { Object result = ((IJavaScriptExecutor)driver).ExecuteScript(script, ng_file); if (result != null) { Console.Error.WriteLine(result.ToString()); } else { Console.Error.WriteLine("result is null"); } } catch (InvalidOperationException e) { Console.Error.WriteLine(e.Message); } }
public void ShouldPrintOrderByFieldColumn() { GetPageContent("ng_headers_sort_example2.htm"); String[] headers = new String[] { "First Name", "Last Name", "Age" }; foreach (String header in headers) { for (int cnt = 0; cnt != 2; cnt++) { IWebElement headerElement = ngDriver.FindElement(By.XPath("//th/a[contains(text(),'" + header + "')]")); Console.Error.WriteLine("Clicking on header: " + header); headerElement.Click(); // triggers ngDriver.WaitForAngular() Assert.IsNotEmpty(ngDriver.Url); ReadOnlyCollection <NgWebElement> ng_emps = ngDriver.FindElements(NgBy.Repeater("emp in data.employees")); NgWebElement ng_emp = ng_emps[0]; String field = ng_emp.GetAttribute("ng-order-by"); Console.Error.WriteLine(field + ": " + ng_emp.Evaluate(field).ToString()); String empField = "emp." + ng_emp.Evaluate(field); Console.Error.WriteLine(empField + ":"); var ng_emp_enumerator = ng_emps.GetEnumerator(); ng_emp_enumerator.Reset(); while (ng_emp_enumerator.MoveNext()) { ng_emp = (NgWebElement)ng_emp_enumerator.Current; if (ng_emp.Text == null) { break; } Assert.IsNotNull(ng_emp.WrappedElement); // Console.Error.WriteLine(ngEmp.getAttribute("innerHTML")); try { NgWebElement ng_column = ng_emp.FindElement(NgBy.Binding(empField)); Assert.IsNotNull(ng_column); Console.Error.WriteLine(ng_column.Text); } catch (Exception ex) { Console.Error.WriteLine(ex.ToString()); } } } } }
public void ShouldEvaluateIf() { Common.GetLocalHostPageContent("ng_watch_ng_if.htm"); IWebElement button = ngDriver.FindElement(By.CssSelector("button.btn")); NgWebElement ng_button = new NgWebElement(ngDriver, button); Object state = ng_button.Evaluate("!house.frontDoor.isOpen"); Assert.IsTrue(Convert.ToBoolean(state)); StringAssert.IsMatch("house.frontDoor.open()", button.GetAttribute("ng-click")); StringAssert.IsMatch("Open Door", button.Text); button.Click(); }
public void ShouldFindOrderByField() { Common.GetLocalHostPageContent("ng_headers_sort_example1.htm"); String[] headers = new String[] { "First Name", "Last Name", "Age" }; foreach (String header in headers) { IWebElement headerelement = ngDriver.FindElement(By.XPath(String.Format("//th/a[contains(text(),'{0}')]", header))); Console.Error.WriteLine(header); headerelement.Click(); // to trigger WaitForAngular Assert.IsNotEmpty(ngDriver.Url); IWebElement emp = ngDriver.FindElement(NgBy.Repeater("emp in data.employees")); NgWebElement ngRow = new NgWebElement(ngDriver, emp); String orderByField = emp.GetAttribute("ng-order-by"); Console.Error.WriteLine(orderByField + ": " + ngRow.Evaluate(orderByField).ToString()); } }
public void ShouldFindOrderByField() { GetPageContent("ng_headers_sort_example1.htm"); String[] headers = new String[] { "First Name", "Last Name", "Age" }; foreach (String header in headers) { IWebElement headerelement = ngDriver.FindElement(By.XPath(String.Format("//th/a[contains(text(),'{0}')]", header))); Console.Error.WriteLine(header); headerelement.Click(); // to trigger WaitForAngular Assert.IsNotEmpty(ngDriver.Url); IWebElement emp = ngDriver.FindElement(NgBy.Repeater("emp in data.employees")); NgWebElement ngRow = new NgWebElement(ngDriver, emp); String orderByField = emp.GetAttribute("ng-order-by"); Console.Error.WriteLine(orderByField + ": " + ngRow.Evaluate(orderByField).ToString()); } ReadOnlyCollection <NgWebElement> ng_people = ngDriver.FindElements(NgBy.Repeater("person in people")); var ng_people_enumerator = ng_people.GetEnumerator(); ng_people_enumerator.Reset(); while (ng_people_enumerator.MoveNext()) { NgWebElement ng_person = (NgWebElement)ng_people_enumerator.Current; if (ng_person.Text == null) { break; } NgWebElement ng_name = ng_person.FindElement(NgBy.Binding("person.Name")); Assert.IsNotNull(ng_name.WrappedElement); Object obj_country = ng_person.Evaluate("person.Country"); Assert.IsNotNull(obj_country); if (String.Compare("Around the Horn", ng_name.Text) == 0) { StringAssert.IsMatch("UK", obj_country.ToString()); } } }
public void Should_01_EvaluateIf() { driver.Navigate().GoToUrl(base_url); IWebElement frameElement = driver.FindElement(By.CssSelector("iframe[id='cp_embed_EjYzev']")); actions.MoveToElement(frameElement).Build().Perform(); driver.Highlight(frameElement); var tmp = driver.SwitchTo().Frame(frameElement); frameElement = tmp.FindElement(By.XPath("//iframe[@id='result-iframe']")); frame = tmp.SwitchTo().Frame(frameElement); IWebElement button = frame.FindElement(By.CssSelector("button.btn")); ngDriver = new NgWebDriver(frame); ngDriver.IgnoreSynchronization = false; NgWebElement ng_button = new NgWebElement(ngDriver, button); Object state = ng_button.Evaluate("!house.frontDoor.isOpen"); Assert.IsTrue(Convert.ToBoolean(state)); StringAssert.IsMatch("house.frontDoor.open()", button.GetAttribute("ng-click")); StringAssert.IsMatch("Open Door", button.Text); }
public void ShouldDeleteCustomer() { // switch to "Add Customer" screen ngDriver.FindElement(NgBy.ButtonText("Bank Manager Login")).Click(); ngDriver.FindElement(NgBy.PartialButtonText("Add Customer")).Click(); // fill new Customer data ngDriver.FindElement(NgBy.Model("fName")).SendKeys("John"); ngDriver.FindElement(NgBy.Model("lName")).SendKeys("Doe"); ngDriver.FindElement(NgBy.Model("postCd")).SendKeys("11011"); // NOTE: there are two 'Add Customer' buttons on this form NgWebElement ng_add_customer_button = ngDriver.FindElements(NgBy.PartialButtonText("Add Customer"))[1]; actions.MoveToElement(ng_add_customer_button.WrappedElement).Build().Perform(); ngDriver.Highlight(ng_add_customer_button, highlight_timeout); ng_add_customer_button.Submit(); // confirm ngDriver.WrappedDriver.SwitchTo().Alert().Accept(); // switch to "Home" screen ngDriver.FindElement(NgBy.ButtonText("Home")).Click(); ngDriver.FindElement(NgBy.ButtonText("Bank Manager Login")).Click(); ngDriver.FindElement(NgBy.PartialButtonText("Customers")).Click(); // found new customer ReadOnlyCollection <NgWebElement> ng_customers = ngDriver.FindElements(NgBy.Repeater("cust in Customers")); // collect all customers ReadOnlyCollection <NgWebElement> ng_custfNames = ngDriver.FindElements(NgBy.RepeaterColumn("cust in Customers", "cust.fName")); // In the application there is always 5 customers preloaded: // http://www.way2automation.com/angularjs-protractor/banking/mockDataLoadService.js Assert.Greater(ng_custfNames.Count, 3); NgWebElement new_customer = ng_customers.Single(cust => Regex.IsMatch(cust.Text, "Harry Potter")); Assert.IsNotNull(new_customer); ReadOnlyCollection <Object> accounts = (ReadOnlyCollection <Object>)new_customer.Evaluate("cust.accountNo"); foreach (Object account in accounts) { Console.Error.WriteLine("AccountNo: {0}", account.ToString()); } // highlight individual accounts of an existing customer ReadOnlyCollection <NgWebElement> ng_accounts = ng_customers.First().FindElements(NgBy.Repeater("account in cust.accountNo")); foreach (NgWebElement ng_account in ng_accounts) { ngDriver.Highlight(ng_account, highlight_timeout); } // remove customer that was just added NgWebElement ng_delete_customer = ng_customers.Single(cust => Regex.IsMatch(cust.Text, "John Doe")); Assert.IsNotNull(ng_delete_customer); actions.MoveToElement(ng_delete_customer.WrappedElement).Build().Perform(); ngDriver.Highlight(ng_delete_customer, highlight_timeout); Thread.Sleep(1000); // locate the remove button NgWebElement ng_delete_customer_button = ng_delete_customer.FindElement(NgBy.ButtonText("Delete")); StringAssert.IsMatch("Delete", ng_delete_customer_button.Text); ngDriver.Highlight(ng_delete_customer_button, highlight_timeout); actions.MoveToElement(ng_delete_customer_button.WrappedElement).ClickAndHold().Build().Perform(); Thread.Sleep(1000); actions.Release().Build().Perform(); // confirm the customer is gone ng_customers = ngDriver.FindElements(NgBy.Repeater("cust in Customers")); IEnumerable <NgWebElement> removed_customer = ng_customers.TakeWhile(cust => Regex.IsMatch(cust.Text, "John Doe.*")); Assert.IsEmpty(removed_customer); }
public void ShouldInviteToOpenAccount() { // When I proceed to "Bank Manager Login" ngDriver.FindElement(NgBy.ButtonText("Bank Manager Login")).Click(); // And I proceed to "Add Customer" ngDriver.FindElement(NgBy.PartialButtonText("Add Customer")).Click(); // And I fill new Customer data IWebElement ng_first_name = ngDriver.FindElement(NgBy.Model("fName")); ngDriver.Highlight(ng_first_name, highlight_timeout); StringAssert.IsMatch("First Name", ng_first_name.GetAttribute("placeholder")); ng_first_name.SendKeys("John"); IWebElement ng_last_name = ngDriver.FindElement(NgBy.Model("lName")); ngDriver.Highlight(ng_last_name, highlight_timeout); StringAssert.IsMatch("Last Name", ng_last_name.GetAttribute("placeholder")); ng_last_name.SendKeys("Doe"); IWebElement ng_post_code = ngDriver.FindElement(NgBy.Model("postCd")); ngDriver.Highlight(ng_post_code, highlight_timeout); StringAssert.IsMatch("Post Code", ng_post_code.GetAttribute("placeholder")); ng_post_code.SendKeys("11011"); // NOTE: there are two 'Add Customer' buttons on this form NgWebElement ng_add_customer_button = ngDriver.FindElements(NgBy.PartialButtonText("Add Customer"))[1]; actions.MoveToElement(ng_add_customer_button.WrappedElement).Build().Perform(); ngDriver.Highlight(ng_add_customer_button, highlight_timeout); ng_add_customer_button.Submit(); // confirm string alert_text = null; try { alert = ngDriver.WrappedDriver.SwitchTo().Alert(); alert_text = alert.Text; StringAssert.StartsWith("Customer added successfully with customer id :", alert_text); alert.Accept(); } catch (NoAlertPresentException ex) { // Alert not present verificationErrors.Append(ex.StackTrace); } catch (WebDriverException ex) { // Alert not handled by PhantomJS verificationErrors.Append(ex.StackTrace); } int customer_id = 0; int.TryParse(alert_text.FindMatch(@"(?<customer_id>\d+)$"), out customer_id); Assert.AreNotEqual(0, customer_id); // And I switch to "Home" screen ngDriver.FindElement(NgBy.ButtonText("Home")).Click(); // And I proceed to "Customer Login" ngDriver.FindElement(NgBy.ButtonText("Customer Login")).Click(); // And I login as new customer "John Doe" ReadOnlyCollection <NgWebElement> ng_customers = ngDriver.FindElements(NgBy.Repeater("cust in Customers")); int customer_count = ng_customers.Count; NgWebElement ng_new_customer = ng_customers.First(cust => Regex.IsMatch(cust.Text, "John Doe")); Assert.IsNotNull(ng_new_customer); actions.MoveToElement(ng_new_customer.WrappedElement).Build().Perform(); ngDriver.Highlight(ng_new_customer, highlight_timeout); ng_new_customer.Click(); NgWebElement ng_login_button = ngDriver.FindElement(NgBy.ButtonText("Login")); Assert.IsTrue(ng_login_button.Displayed && ng_login_button.Enabled); ngDriver.Highlight(ng_login_button, highlight_timeout); ng_login_button.Click(); // Then I am greeted as "John Doe" NgWebElement ng_user = ngDriver.FindElement(NgBy.Binding("user")); StringAssert.Contains("John", ng_user.Text); StringAssert.Contains("Doe", ng_user.Text); // And I am invited to open an account Object noAccount = ng_user.Evaluate("noAccount"); Assert.IsTrue(Boolean.Parse(noAccount.ToString())); Boolean hasAccounts = !(Boolean.Parse(noAccount.ToString())); Console.Error.WriteLine("Has accounts: " + hasAccounts); // IWebElement invitationMessage = driver.FindElement(By.CssSelector("span[ng-show='noAccount']")); IWebElement invitationMessage = ng_user.FindElement(By.XPath("..")).FindElement(By.XPath("..")).FindElement(By.CssSelector("span[ng-show='noAccount']")); Assert.IsTrue(invitationMessage.Displayed); ngDriver.Highlight(invitationMessage); StringAssert.Contains("Please open an account with us", invitationMessage.Text); Console.Error.WriteLine(invitationMessage.Text); // And I have no accounts NgWebElement accountNo = ngDriver.FindElement(NgBy.Binding("accountNo")); Assert.IsFalse(accountNo.Displayed); ReadOnlyCollection <NgWebElement> ng_accounts = ngDriver.FindElements(NgBy.Repeater("account for account in Accounts")); Assert.AreEqual(0, ng_accounts.Count); }
public void ShouldUpload() { // GetPageContent("ng_upload1.htm"); // need to run ngDriver.Navigate().GoToUrl("http://localhost:8080/ng_upload1.htm"); IWebElement file = driver.FindElement(By.CssSelector("div[ng-controller = 'myCtrl'] > input[type='file']")); Assert.IsNotNull(file); StringAssert.AreEqualIgnoringCase(file.GetAttribute("file-model"), "myFile"); String localPath = CreateTempFile("lorem ipsum dolor sit amet"); IAllowsFileDetection fileDetectionDriver = driver as IAllowsFileDetection; if (fileDetectionDriver == null) { Assert.Fail("driver does not support file detection. This should not be"); } fileDetectionDriver.FileDetector = new LocalFileDetector(); try { file.SendKeys(localPath); } catch (WebDriverException e) { // the operation has timed out Console.Error.WriteLine(e.Message); } NgWebElement button = ngDriver.FindElement(NgBy.ButtonText("Upload")); button.Click(); NgWebElement ng_file = new NgWebElement(ngDriver, file); Object myFile = ng_file.Evaluate("myFile"); if (myFile != null) { Dictionary<String, Object> result = (Dictionary<String, Object>)myFile; Assert.IsTrue(result.Keys.Contains("name")); Assert.IsTrue(result.Keys.Contains("type")); Assert.IsTrue(result.Keys.Contains("size")); } else { Console.Error.WriteLine("myFile is null"); } String script = "var e = angular.element(arguments[0]); var f = e.scope().myFile; if (f){return f.name} else {return null;}"; try { Object result = ((IJavaScriptExecutor)driver).ExecuteScript(script, ng_file); if (result != null) { Console.Error.WriteLine(result.ToString()); } else { Console.Error.WriteLine("result is null"); } } catch (InvalidOperationException e) { Console.Error.WriteLine(e.Message); } }
public void ShouldFindOrderByField() { GetPageContent("ng_headers_sort_example1.htm"); String[] headers = new String[] { "First Name", "Last Name", "Age" }; foreach (String header in headers) { IWebElement headerelement = ngDriver.FindElement(By.XPath(String.Format("//th/a[contains(text(),'{0}')]", header))); Console.Error.WriteLine(header); headerelement.Click(); // to trigger WaitForAngular Assert.IsNotEmpty(ngDriver.Url); IWebElement emp = ngDriver.FindElement(NgBy.Repeater("emp in data.employees")); NgWebElement ngRow = new NgWebElement(ngDriver, emp); String orderByField = emp.GetAttribute("ng-order-by"); Console.Error.WriteLine(orderByField + ": " + ngRow.Evaluate(orderByField).ToString()); } ReadOnlyCollection<NgWebElement> ng_people = ngDriver.FindElements(NgBy.Repeater("person in people")); var ng_people_enumerator = ng_people.GetEnumerator(); ng_people_enumerator.Reset(); while (ng_people_enumerator.MoveNext()) { NgWebElement ng_person = (NgWebElement)ng_people_enumerator.Current; if (ng_person.Text == null) { break; } NgWebElement ng_name = ng_person.FindElement(NgBy.Binding("person.Name")); Assert.IsNotNull(ng_name.WrappedElement); Object obj_country = ng_person.Evaluate("person.Country"); Assert.IsNotNull(obj_country); if (String.Compare("Around the Horn", ng_name.Text) == 0) { StringAssert.IsMatch("UK", obj_country.ToString()); } } }
public void ShouldEvaluateIf() { GetPageContent("ng_watch_ng_if.htm"); IWebElement button = ngDriver.FindElement(By.CssSelector("button.btn")); NgWebElement ng_button = new NgWebElement(ngDriver, button); Object state = ng_button.Evaluate("!house.frontDoor.isOpen"); Assert.IsTrue(Convert.ToBoolean(state)); StringAssert.IsMatch("house.frontDoor.open()", button.GetAttribute("ng-click")); StringAssert.IsMatch("Open Door", button.Text); button.Click(); }
public void Should_SelectSingle() { wait.Until(ExpectedConditions.ElementExists(By.CssSelector( "body > nav button i.icon-play"))); IWebElement buttonElement = driver.FindElement(By.CssSelector("body > nav button i.icon-play")); Assert.IsNotNull(buttonElement); driver.Highlight(buttonElement); buttonElement.Click(); // // IWebElement frameElement = driver.FindElement(By.CssSelector("iframe[name='plunkerPreviewTarget']")); Assert.IsNotNull(frameElement); iframe = driver.SwitchTo().Frame(frameElement); String headerText = "Single select example"; Thread.Sleep(1500); wait.Until(ExpectedConditions.ElementExists(By.XPath( String.Format("//form/div[contains(text(), '{0}')]", headerText)))); IWebElement header = iframe.FindElement(By.XPath( String.Format("//form/div[contains(text(), '{0}')]", headerText))); Assert.IsNotNull(header); actions.MoveToElement(header).Build().Perform(); driver.Highlight(header); IWebElement selectOne = iframe.FindElement(By.XPath("//ng-select")); Assert.IsNotNull(selectOne); actions.MoveToElement(selectOne).Build().Perform(); driver.Highlight(selectOne); Console.Error.WriteLine("Element contents:\n{0}", selectOne.GetAttribute("innerHTML")); String buttonText = "Select one"; IWebElement selectButtonElement = selectOne.FindElement(By.XPath(String.Format("//div[@class='placeholder'][contains(text(), '{0}')]", buttonText))); Assert.IsNotNull(selectButtonElement); actions.MoveToElement(selectButtonElement).Build().Perform(); driver.Highlight(selectButtonElement); selectButtonElement.Click(); wait.Until(d => (d.FindElements(By.CssSelector("select-dropdown div.options ul li")).Count > 0)); IWebElement dropdownElement = iframe.FindElement(By.CssSelector("select-dropdown div.options")); Assert.IsNotNull(dropdownElement); IWebElement[] optionElements = dropdownElement.FindElements(By.CssSelector("ul li")).ToArray(); Assert.IsTrue(1 <= optionElements.Length); foreach (IWebElement optionElement in optionElements) { actions.MoveToElement(optionElement).Build().Perform(); if (optionElement.Text.Contains("10")) { Console.Error.WriteLine("Selecting option:\"{0}\"", optionElement.Text); driver.Highlight(optionElement); optionElement.Click(); } try { NgWebElement ng_option_element = new NgWebElement(ngDriver, optionElement); Assert.IsNotNull(ng_option_element.WrappedElement); Console.Error.WriteLine("Option angular object:{0}\n", ng_option_element.Evaluate("ng-reflect-ng-outlet-context")); } catch (InvalidOperationException e) { // angular is not defined Console.Error.WriteLine("Ignore exception: " + e.Message); } catch (StaleElementReferenceException) { break; } } IWebElement selectOptions = iframe.FindElement(By.XPath("//ng-select[@formcontrolname='selectSingle']/following-sibling::div")); Assert.IsNotNull(selectOptions); actions.MoveToElement(selectOptions).Build().Perform(); driver.Highlight(selectOptions); String idPattern = @"Selected option id: (?<result>\d{1,2})"; Assert.IsTrue((new Regex(idPattern)).IsMatch(selectOptions.Text)); int result = 0; int.TryParse(selectOptions.Text.FindMatch(idPattern), out result); Assert.AreEqual(10, result); Console.Error.WriteLine("FindMatch result: {0}\n", result.ToString()); }