Created
March 23, 2019 17:20
-
-
Save pragmatictesters/16813c91075645c2014366221ad4a4e4 to your computer and use it in GitHub Desktop.
Selenium WebDriver C# : Sample Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using OpenQA.Selenium; | |
using OpenQA.Selenium.Chrome; | |
using OpenQA.Selenium.Interactions; | |
using OpenQA.Selenium.Support.UI; | |
using System; | |
using System.Collections.Generic; | |
namespace ISMDemoApplication | |
{ | |
[TestClass] | |
public class LoginTests | |
{ | |
public IWebDriver driver; | |
public String BASE_URL = "https://demo-us.sana-commerce.com/en-us"; | |
//public String BASE_URL = "https://the-internet.herokuapp.com/javascript_alerts"; | |
[TestInitialize] | |
public void SetUp() | |
{ | |
ChromeOptions options = new ChromeOptions(); | |
options.AddArgument("--start-maximized"); | |
options.AddArguments("--disable-infobars"); | |
driver = new ChromeDriver(options); | |
driver.Navigate().GoToUrl(BASE_URL); | |
} | |
[TestCleanup] | |
public void TearDown() | |
{ | |
//driver.Close(); | |
} | |
[TestMethod] | |
public void TestUserLoginWithInvalidCredential() | |
{ | |
string UserName = "[email protected]"; | |
string Password = "InvalidPassword"; | |
string ExpectedErrorMessage = "Your login attempt was unsuccessful. Please try again."; | |
string ActualError; | |
//Click the login link | |
driver.FindElement(By.CssSelector("a.top-hyp.font-smaller")).Click(); | |
driver.FindElement(By.CssSelector("#UserName")).SendKeys(UserName); | |
driver.FindElement(By.CssSelector("#Password")).SendKeys(Password); | |
driver.FindElement(By.CssSelector(".btn-login > .btn-cnt")).Click(); | |
ActualError = driver.FindElement(By.CssSelector(".validation-summary-errors span")).Text; | |
Assert.AreEqual(ExpectedErrorMessage, ActualError); | |
} | |
[TestMethod] | |
public void TestUserLoginWithInvalidCredntialsPageObject() | |
{ | |
string UserName = "[email protected]"; | |
string Password = "InvalidPassword"; | |
string ExpectedErrorMessage = "Your login attempt was unsuccessful. Please try again."; | |
//Click the login link | |
driver.FindElement(By.CssSelector("a.top-hyp.font-smaller")).Click(); | |
LoginPage loginPage = new LoginPage(driver); | |
loginPage.typeUsernme(UserName).typePassword(Password).clickLoginFailure(); | |
Assert.AreEqual(ExpectedErrorMessage, loginPage.getValidationError()); | |
} | |
[TestMethod] | |
public void TestJavaScriptAlert() | |
{ | |
String expectedConfirmation = "You successfuly clicked an alert"; | |
String expectedAlertMessage = "I am a JS Alert"; | |
//Click on the button | |
driver.FindElement(By.CssSelector("li:nth-child(1) > button")).Click(); | |
Assert.AreEqual(expectedAlertMessage, driver.SwitchTo().Alert().Text); | |
driver.SwitchTo().Alert().Accept(); | |
String actualConfirmationMessage = driver.FindElement(By.CssSelector("#result")).Text; | |
Assert.AreEqual(expectedConfirmation, actualConfirmationMessage); | |
} | |
[TestMethod] | |
public void TestJavaScriptConfirmationOK() | |
{ | |
String expectedConfirmationOK = "You clicked: Ok"; | |
String expectedAlertMessage = "I am a JS Confirm"; | |
//Click on the button | |
driver.FindElement(By.CssSelector("li:nth-child(2) > button")).Click(); | |
Assert.AreEqual(expectedAlertMessage, driver.SwitchTo().Alert().Text); | |
driver.SwitchTo().Alert().Accept(); | |
String actualConfirmationMessage = driver.FindElement(By.CssSelector("#result")).Text; | |
Assert.AreEqual(expectedConfirmationOK, actualConfirmationMessage); | |
} | |
[TestMethod] | |
public void TestJavaScriptConfirmationCANCEL() | |
{ | |
String expectedConfirmationCANCEL = "You clicked: Cancel"; | |
String expectedAlertMessage = "I am a JS Confirm"; | |
//Click on the button | |
driver.FindElement(By.CssSelector("li:nth-child(2) > button")).Click(); | |
Assert.AreEqual(expectedAlertMessage, driver.SwitchTo().Alert().Text); | |
driver.SwitchTo().Alert().Dismiss(); | |
String actualConfirmationMessage = driver.FindElement(By.CssSelector("#result")).Text; | |
Assert.AreEqual(expectedConfirmationCANCEL, actualConfirmationMessage); | |
} | |
[TestMethod] | |
public void TestJavaScriptPromptOK() | |
{ | |
String expectedAlertMessage = "I am a JS prompt"; | |
String strInputText = "Hello Selenium"; | |
String expectedConfirmation = "You entered: " + strInputText; | |
//Click on the button | |
driver.FindElement(By.CssSelector("li:nth-child(3) > button")).Click(); | |
Assert.AreEqual(expectedAlertMessage, driver.SwitchTo().Alert().Text); | |
driver.SwitchTo().Alert().SendKeys(strInputText); | |
driver.SwitchTo().Alert().Accept(); | |
String actualConfirmationMessage = driver.FindElement(By.CssSelector("#result")).Text; | |
Assert.AreEqual(expectedConfirmation, actualConfirmationMessage); | |
} | |
[TestMethod] | |
public void TestJavaScriptPromptCANCEL() | |
{ | |
String expectedAlertMessage = "I am a JS prompt"; | |
String strInputText = "Hello Selenium"; | |
String expectedConfirmation = "You entered: null"; | |
//Click on the button | |
driver.FindElement(By.CssSelector("li:nth-child(3) > button")).Click(); | |
Assert.AreEqual(expectedAlertMessage, driver.SwitchTo().Alert().Text); | |
driver.SwitchTo().Alert().SendKeys(strInputText); | |
driver.SwitchTo().Alert().Dismiss(); | |
String actualConfirmationMessage = driver.FindElement(By.CssSelector("#result")).Text; | |
Assert.AreEqual(expectedConfirmation, actualConfirmationMessage); | |
} | |
[TestMethod] | |
public void TestCreatingANewAccountValidationErrors() | |
{ | |
driver.FindElement(By.CssSelector("a.top-hyp.font-smaller")).Click(); | |
driver.FindElement(By.CssSelector("a .btn-cnt")).Click(); | |
driver.FindElement(By.CssSelector("[title='Submit'] .btn-cnt")).Click(); | |
Assert.AreEqual("Name is required.", driver.FindElement(By.CssSelector("#Address_Name-error")).Text); | |
Assert.AreEqual("Country is required.", driver.FindElement(By.CssSelector("#Address_CountryId-error")).Text); | |
Assert.AreEqual("City is required.", driver.FindElement(By.CssSelector("#Address_City-error")).Text); | |
Assert.AreEqual("Address is required.", driver.FindElement(By.CssSelector("#Address_Address-error")).Text); | |
Assert.AreEqual("Zip code is required.", driver.FindElement(By.CssSelector("#Address_ZipCode-error")).Text); | |
Assert.AreEqual("E-mail address is required.", driver.FindElement(By.CssSelector("#LoginData_UserName-error")).Text); | |
Assert.AreEqual("Password is required.", driver.FindElement(By.CssSelector("#LoginData_Password-error")).Text); | |
Assert.AreEqual("Confirm password is required.", driver.FindElement(By.CssSelector("#LoginData_ConfirmPassword-error")).Text); | |
} | |
[TestMethod] | |
public void TestCreatingANewAccount() | |
{ | |
driver.FindElement(By.CssSelector("a.top-hyp.font-smaller")).Click(); | |
driver.FindElement(By.CssSelector("a .btn-cnt")).Click(); | |
driver.FindElement(By.CssSelector("#Address_Name")).SendKeys("Janesh Kodikara"); | |
driver.FindElement(By.CssSelector("#LoginData_UserName")).SendKeys("[email protected]"); | |
//Select a country from the drop down | |
driver.FindElement(By.CssSelector("#Address_CountryId-button > .ui-selectmenu-icon")).Click(); | |
driver.FindElement(By.CssSelector("#ui-id-5")).Click(); | |
//Select a state from the drop down | |
driver.FindElement(By.CssSelector(".selectmenu-placeholder")).Click(); | |
driver.FindElement(By.CssSelector("#ui-id-24")).Click(); | |
driver.FindElement(By.CssSelector("#Address_City")).SendKeys("Colombo"); | |
driver.FindElement(By.CssSelector("#Address_Address")).SendKeys("Orion City"); | |
driver.FindElement(By.CssSelector("#Address_Address2")).SendKeys("Block A"); | |
driver.FindElement(By.CssSelector("#Address_ZipCode")).SendKeys("GQ1008"); | |
driver.FindElement(By.CssSelector("#Address_PhoneNo")).SendKeys("0718762325"); | |
driver.FindElement(By.CssSelector("#LoginData_Password")).SendKeys("Test-0987"); | |
driver.FindElement(By.CssSelector("#LoginData_ConfirmPassword")).SendKeys("Test-0987"); | |
driver.FindElement(By.CssSelector("[title='Submit'] .btn-cnt")).Click(); | |
} | |
[TestMethod] | |
public void TestLoginPage() | |
{ | |
driver.FindElement(By.CssSelector("a.top-hyp.font-smaller")).Click(); | |
//Verify hyper link | |
//< a href = "/en-us/profile/forgotpassword" tabindex = "-1" > Forgot password </ a > | |
IWebElement lnkForgotPassword = driver.FindElement(By.CssSelector("a[href*='forgotpassword']")); | |
Assert.IsTrue(lnkForgotPassword.Displayed); | |
Assert.IsTrue(lnkForgotPassword.Enabled); | |
Assert.AreEqual("Forgot password", lnkForgotPassword.Text); | |
//Verify checkbox | |
IWebElement chkRememberMe = driver.FindElement(By.CssSelector("#RememberMe")); | |
Assert.IsFalse(chkRememberMe.Selected); | |
Assert.IsTrue(chkRememberMe.Enabled); | |
//Click the check box | |
chkRememberMe.Click(); | |
Assert.IsTrue(chkRememberMe.Selected); | |
Assert.IsTrue(chkRememberMe.Enabled); | |
} | |
[TestMethod] | |
public void TestIdentifyAutoSerachListInSearchBox() | |
{ | |
String searchText = "Test"; | |
driver.FindElement(By.CssSelector("#searchbox")).SendKeys(searchText); | |
System.Threading.Thread.Sleep(5000); | |
// | |
IList<IWebElement> listTitles= driver.FindElements(By.CssSelector(".suggest-text .suggest-title-id")); | |
StringComparison com = StringComparison.OrdinalIgnoreCase; | |
Console.WriteLine("START TEST"); | |
for (int i = 0; i < listTitles.Count; i++) | |
{ | |
// Assert.IsTrue(listTitles[i].Text.Contains(searchText, com)); | |
Console.WriteLine(listTitles[i].Text); | |
} | |
Console.WriteLine("END TEST "); | |
} | |
[TestMethod] | |
public void TestProductListPage() | |
{ | |
driver.FindElement(By.CssSelector("#content-slider li:nth-of-type(2) .btn-cnt")).Click(); | |
driver.FindElement(By.CssSelector("a[href='/en-us/notebooks/brand=sony/']")).Click(); | |
Assert.IsNotNull(driver.FindElement(By.CssSelector(".facet-item.ellipsis img[src*='Sony']"))); | |
} | |
[TestMethod] | |
public void TestMiniBasket() | |
{ | |
driver.FindElement(By.CssSelector("#content-slider li:nth-of-type(2) .btn-cnt")).Click(); | |
driver.FindElement(By.CssSelector("input[value='SC10004'] ~ button")).Click(); | |
IWebElement elOrderSummary= driver.FindElement(By.CssSelector(".over-summary")); | |
Actions builder = new Actions(driver); | |
builder.MoveToElement(elOrderSummary).Build().Perform(); | |
IWebElement elLnkViewShoppingCart = driver.FindElement(By.CssSelector("a[href='/en-us/shop/basket']")); | |
Assert.IsNotNull(elLnkViewShoppingCart); | |
Assert.IsTrue(elLnkViewShoppingCart.Enabled); | |
Assert.IsTrue(elLnkViewShoppingCart.Displayed); | |
} | |
[TestMethod] | |
public void TestPopups() | |
{ | |
//Need to identify the popups | |
} | |
[TestMethod] | |
public void TestOrderOverviewPage() | |
{ | |
driver.FindElement(By.CssSelector("#content-slider li:nth-of-type(2) .btn-cnt")).Click(); | |
driver.FindElement(By.CssSelector("input[value='SC10004'] ~ button")).Click(); | |
IWebElement elOrderSummary = driver.FindElement(By.CssSelector(".over-summary")); | |
Actions builder = new Actions(driver); | |
builder.MoveToElement(elOrderSummary).Build().Perform(); | |
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); | |
wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("a[title='Checkout']"))); | |
driver.FindElement(By.CssSelector("a[title='Checkout']")).Click(); | |
//Login | |
driver.FindElement(By.CssSelector("#UserName")).SendKeys("[email protected]"); | |
driver.FindElement(By.CssSelector("#Password")).SendKeys("Test-0987"); | |
driver.FindElement(By.CssSelector(".btn-login > .btn-cnt")).Click(); | |
IWebElement elRadioDefault = driver.FindElement(By.CssSelector("input[name='shippingChoice'][value='default']")); | |
Assert.IsTrue(elRadioDefault.Selected); | |
driver.FindElement(By.CssSelector("button[type='submit'][tabindex='1']")).Click(); | |
wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("#shippingMethodsTable tr>th"))); | |
String strTableHeader = driver.FindElement(By.CssSelector("#shippingMethodsTable tr>th")).Text; | |
Assert.AreEqual("Delivery method", strTableHeader); | |
} | |
[TestMethod] | |
public void TestDeliveryMethodPage() | |
{ | |
driver.FindElement(By.CssSelector("#content-slider li:nth-of-type(2) .btn-cnt")).Click(); | |
driver.FindElement(By.CssSelector("input[value='SC10004'] ~ button")).Click(); | |
IWebElement elOrderSummary = driver.FindElement(By.CssSelector(".over-summary")); | |
Actions builder = new Actions(driver); | |
builder.MoveToElement(elOrderSummary).Build().Perform(); | |
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); | |
wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("a[title='Checkout']"))); | |
driver.FindElement(By.CssSelector("a[title='Checkout']")).Click(); | |
//Login | |
driver.FindElement(By.CssSelector("#UserName")).SendKeys("[email protected]"); | |
driver.FindElement(By.CssSelector("#Password")).SendKeys("Test-0987"); | |
driver.FindElement(By.CssSelector(".btn-login > .btn-cnt")).Click(); | |
IWebElement elRadioDefault = driver.FindElement(By.CssSelector("input[name='shippingChoice'][value='default']")); | |
Assert.IsTrue(elRadioDefault.Selected); | |
driver.FindElement(By.CssSelector("button[type='submit'][tabindex='1']")).Click(); | |
wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("#shippingMethodsTable tr>th"))); | |
String strTableHeader = driver.FindElement(By.CssSelector("#shippingMethodsTable tr>th")).Text; | |
Assert.AreEqual("Delivery method", strTableHeader); | |
//Click Next button | |
driver.FindElement(By.CssSelector("button[type='submit'][tabindex='1']")).Click(); | |
wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("div.tbx.tbx-date"))); | |
driver.FindElement(By.CssSelector("div.tbx.tbx-date")).Click(); | |
wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("a.ui-datepicker-next"))); | |
driver.FindElement(By.CssSelector("a.ui-datepicker-next")).Click(); | |
driver.FindElement(By.CssSelector("a.ui-state-default")).Click(); | |
} | |
[TestMethod] | |
public void TestCerateReturnOrderPage() | |
{ | |
//TODO | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a sample test code create for Pragmatic Training delegates.