Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
- Refactor of BrowserWindowsPage.cs and AlsertsPage.cs
  • Loading branch information
adamTarasiewicz committed Sep 29, 2023
1 parent 8c01181 commit a1437c6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 75 deletions.
86 changes: 36 additions & 50 deletions CSharp_Selenium_DemoQA/Pages/Alerts, Frame & Windows/AlertsPage.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
using System;

namespace CSharp_Selenium_DemoQA.Tests
{
Expand All @@ -10,81 +11,66 @@ public AlertsPage(IWebDriver driver) : base(driver)
{
}

// Elements
public IWebElement FirstAlertButton => Driver.FindElement(By.Id("alertButton"));
public IWebElement SecondTimerAlertButton => Driver.FindElement(By.Id("timerAlertButton"));
public IWebElement ThirdConfirmButton => Driver.FindElement(By.Id("confirmButton"));
public IWebElement FourthPromtButton => Driver.FindElement(By.Id("promtButton"));
public IWebElement ThirdConfirmButtonAssert => Driver.FindElement(By.Id("confirmResult"));
public IWebElement FourthPromtButtonAssert => Driver.FindElement(By.Id("promptResult"));

internal void CheckAlerts(TestUser user)
// Wait for Alert and Return
private IAlert WaitForAlert()
{
//1 FirstAlertButton
FirstAlertButton.Click();
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.AlertIsPresent());
IAlert alert = Driver.SwitchTo().Alert();
return Driver.SwitchTo().Alert();
}

// Assert Alert Text
private void AssertAlertText(IAlert alert, string expectedText)
{
string alertText = alert.Text;
Assert.AreEqual("You clicked a button", alertText, "Alert text message mismatch");
alert.Accept();

Driver.SwitchTo().DefaultContent();

Assert.AreEqual(expectedText, alertText, "Alert text message mismatch");
}

internal void CheckAlerts(TestUser user)
{
// FirstAlertButton
FirstAlertButton.Click();
IAlert alert = WaitForAlert();
AssertAlertText(alert, "You clicked a button");
alert.Accept();

//2 SecondTimerAlertButton
// SecondTimerAlertButton
SecondTimerAlertButton.Click();
wait.Until(ExpectedConditions.AlertIsPresent());
IAlert secondAlert = Driver.SwitchTo().Alert();

string secondAlertText = secondAlert.Text;
Assert.AreEqual("This alert appeared after 5 seconds", secondAlertText, "Alert text message mismatch");
secondAlert.Accept();

Driver.SwitchTo().DefaultContent();


alert = WaitForAlert();
AssertAlertText(alert, "This alert appeared after 5 seconds");
alert.Accept();

//3 ThirdConfirmButton
// ThirdConfirmButton - OK
ThirdConfirmButton.Click();
wait.Until(ExpectedConditions.AlertIsPresent());
IAlert thirdAlertOK = Driver.SwitchTo().Alert();
thirdAlertOK.Accept();

string thirdAlertOKText = ThirdConfirmButtonAssert.Text;
Assert.IsTrue(thirdAlertOKText.Contains("Ok"), "The expected text 'Ok' was not found in the span element.");

alert = WaitForAlert();
alert.Accept();
Assert.IsTrue(ThirdConfirmButtonAssert.Text.Contains("Ok"), "The expected text 'Ok' was not found in the span element.");

// ThirdConfirmButton - Cancel
ThirdConfirmButton.Click();
wait.Until(ExpectedConditions.AlertIsPresent());
IAlert thirdAlertCancel = Driver.SwitchTo().Alert();
thirdAlertCancel.Dismiss();

string thirdAlertCancelText = ThirdConfirmButtonAssert.Text;
Assert.IsTrue(thirdAlertCancelText.Contains("Cancel"), $"The expected text 'Cancel' was not found in the span element. Found {thirdAlertCancelText} instad.");

Driver.SwitchTo().DefaultContent();
alert = WaitForAlert();
alert.Dismiss();
Assert.IsTrue(ThirdConfirmButtonAssert.Text.Contains("Cancel"), $"The expected text 'Cancel' was not found in the span element. Found {ThirdConfirmButtonAssert.Text} instead.");


//4 FourthPromtButton
// FourthPromtButton
FourthPromtButton.Click();
wait.Until(ExpectedConditions.AlertIsPresent());
IAlert fourthAlert = Driver.SwitchTo().Alert();

fourthAlert.SendKeys(user.FullName);

fourthAlert.Accept();

string fourthAlertOKText = FourthPromtButtonAssert.Text;
Assert.IsTrue(fourthAlertOKText.Contains("Ken Block"), $"The expected text 'Ken Block' was not found in the span element. Found {fourthAlertOKText} instead.");
Driver.SwitchTo().DefaultContent();

alert = WaitForAlert();
alert.SendKeys(user.FullName);
alert.Accept();
Assert.IsTrue(FourthPromtButtonAssert.Text.Contains(user.FullName), $"The expected text '{user.FullName}' was not found in the span element. Found {FourthPromtButtonAssert.Text} instead.");
}

internal void GoTo()
{
Driver.Navigate().GoToUrl("https://demoqa.com/alerts");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;

namespace CSharp_Selenium_DemoQA.Tests
{
Expand All @@ -9,46 +10,53 @@ public BrowserWindowsPage(IWebDriver driver) : base(driver)
{
}

// Elements
public IWebElement NewTabButton => Driver.FindElement(By.Id("tabButton"));
public IWebElement NewWindowButton => Driver.FindElement(By.Id("windowButton"));
public IWebElement NewWindowMessageButton => Driver.FindElement(By.Id("messageWindowButton"));
public IWebElement NewTabButtonAndNewWindowAssert => Driver.FindElement(By.Id("sampleHeading"));

internal void CheckNewTab()
// Switch to New Window/Tab
private void SwitchToNewWindow(int initialWindowCount)
{
int initialWindowCount = Driver.WindowHandles.Count;

NewTabButton.Click();

WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(5));
wait.Until(d => d.WindowHandles.Count > initialWindowCount);
Driver.SwitchTo().Window(Driver.WindowHandles.Last());
}

if (Driver.WindowHandles.Count > initialWindowCount)
{
// Switch to the new tab
Driver.SwitchTo().Window(Driver.WindowHandles.Last());
Assert.AreEqual("This is a sample page", NewTabButtonAndNewWindowAssert.Text, "Text mismatch");
// Switch back to the original tab
Driver.SwitchTo().Window(Driver.WindowHandles.First());
}
// Assert Text in New Window/Tab
private void AssertTextInNewWindow(string expectedText)
{
Assert.AreEqual(expectedText, NewTabButtonAndNewWindowAssert.Text, "Text mismatch");
}

NewWindowButton.Click();
wait.Until(d => d.WindowHandles.Count > initialWindowCount);
// Close New Window and Switch to Original
private void CloseNewWindowAndSwitchToOriginal()
{
Driver.Close();
Driver.SwitchTo().Window(Driver.WindowHandles.First());
}

if (Driver.WindowHandles.Count > initialWindowCount)
{
Driver.SwitchTo().Window(Driver.WindowHandles.Last());
internal void CheckNewTab()
{
int initialWindowCount = Driver.WindowHandles.Count;

Assert.AreEqual("This is a sample page", NewTabButtonAndNewWindowAssert.Text, "Text mismatch");
// Check New Tab
NewTabButton.Click();
SwitchToNewWindow(initialWindowCount);
AssertTextInNewWindow("This is a sample page");
Driver.SwitchTo().Window(Driver.WindowHandles.First());

Driver.Close();
Driver.SwitchTo().Window(Driver.WindowHandles.First());
}
// Check New Window
NewWindowButton.Click();
SwitchToNewWindow(initialWindowCount);
AssertTextInNewWindow("This is a sample page");
CloseNewWindowAndSwitchToOriginal();
}

internal void GoTo()
{
Driver.Navigate().GoToUrl("https://demoqa.com/browser-windows");
}
}
}
}
4 changes: 2 additions & 2 deletions CSharp_Selenium_DemoQA/Tests/AlertsFrameWindowsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ private IWebDriver GetChromeDriver()
{
var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--headless"); // unlock for CI
chromeOptions.AddArgument("--window-size=1920,1080"); // unlock for CI
//chromeOptions.AddArgument("--headless"); // unlock for CI
//chromeOptions.AddArgument("--window-size=1920,1080"); // unlock for CI
return new ChromeDriver(outPutDirectory, chromeOptions);
}
}
Expand Down

0 comments on commit a1437c6

Please sign in to comment.