w ItemPageObject.cs
public void CheckForNotExsitingAction(IWebDriver webDriver, ISearchContext context, string notExsistingActionName)
{
var actions = webDriver.FindElement(By.ClassName("dropdown--square"));
Actions builder = new Actions(webDriver);
builder.MoveToElement(actions).Perform();
new WebDriverWait(webDriver, TimeSpan.FromSeconds(10)).Until(
ExpectedConditions.PresenceOfAllElementsLocatedBy((By.ClassName("dropdown__action"))));
WebDriverUtil.AssertElementNotPresent(context, By.LinkText(notExsistingActionName), "xxx");
}
WebDriverUtil.cs
using System;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
namespace RegistrationForm.Tests.Acceptance
{
public abstract class WebDriverUtil
{
public IWebDriver Driver { get; set; }
public void ClearAndType(IWebElement element, string value)
{
element.Clear();
element.SendKeys(value);
}
public void WaitUpTo(int milliseconds, Func<bool> Condition, string description)
{
int timeElapsed = 0;
while (!Condition() && timeElapsed < milliseconds)
{
Thread.Sleep(100);
timeElapsed += 100;
}
if (timeElapsed >= milliseconds || !Condition())
{
throw new AssertFailedException("Timed out while waiting for: " + description);
}
}
public static void AssertIsEqual(string expectedValue, string actualValue, string elementDescription)
{
if (expectedValue != actualValue)
{
throw new AssertFailedException(String.Format("AssertIsEqual Failed: '{0}' didn't match expectations. Expected [{1}], Actual [{2}]", elementDescription, expectedValue, actualValue));
}
}
public static bool IsElementPresent(IWebElement element)
{
try
{
bool b = element.Displayed;
return b;
}
catch
{
return false;
}
}
public static void AssertElementPresent(IWebElement element, string elementDescription)
{
if (!IsElementPresent(element))
throw new AssertFailedException(String.Format("AssertElementPresent Failed: Could not find '{0}'", elementDescription));
}
public static void AssertElementNotPresent(IWebElement element, string elementDescription)
{
if (IsElementPresent(element))
throw new AssertFailedException(String.Format("AssertElementPresent Failed: Could not find '{0}'", elementDescription));
}
public static bool IsElementPresent(ISearchContext context, By searchBy)
{
try
{
bool b = context.FindElement(searchBy).Displayed;
return b;
}
catch
{
return false;
}
}
public static void AssertElementPresent(ISearchContext context, By searchBy, string elementDescription)
{
if (!IsElementPresent(context, searchBy))
throw new AssertFailedException(String.Format("AssertElementPresent Failed: Could not find '{0}'", elementDescription));
}
public static void AssertElementNotPresent(ISearchContext context, By searchBy, string elementDescription)
{
if (IsElementPresent(context, searchBy))
throw new AssertFailedException(String.Format("AssertElementNotPresent Failed: Could not find '{0}'", elementDescription));
}
public static void AssertElementsPresent(IWebElement[] elements, string elementDescription)
{
if (elements.Length == 0)
throw new AssertFailedException(String.Format("AssertElementsPresent Failed: Could not find '{0}'", elementDescription));
}
public static void AssertElementText(IWebElement element, string expectedValue, string elementDescription)
{
AssertElementPresent(element, elementDescription);
string actualtext = element.Text;
if (actualtext != expectedValue)
{
throw new AssertFailedException(String.Format("AssertElementText Failed: Value for '{0}' did not match expectations. Expected: [{1}], Actual: [{2}]", elementDescription, expectedValue, actualtext));
}
}
public static bool isAlertPresent(IWebDriver webdriver)
{
try
{
webdriver.SwitchTo().Alert();
return true;
}
catch (NoAlertPresentException Ex)
{
return false;
}
}
public static void WaitUntilDocumentIsReady(IWebDriver WebDriver, TimeSpan timeout)
{
try
{
var javaScriptExecutor = WebDriver as IJavaScriptExecutor;
var wait = new WebDriverWait(WebDriver, timeout);
// Check if document is ready
Func<IWebDriver, object> readyCondition = webDriver => javaScriptExecutor
.ExecuteScript("return (document.readyState == 'complete' && jQuery.active == 0)");
wait.Until(readyCondition);
}
catch
{
Thread.Sleep(200);
}
}
}
}
W WorkflowStepDefinition.cs
Przy czym tak naprawdę działa tylko dobrze ten 1 przypadek gdy np. na uprawnienia authora zaloguję się jako TR wtedy mam button open.
[Then(@"I am expecting 1 button and not: ""(.*)"" action")]
public void ThenITryToPerformAction(string p0)
{
ItemPageObject item = ScenarioContext.Current.Get<ItemPageObject>("item");
var action = item.SingleActionButton;
WebDriverUtil.AssertElementNotPresent(action, "action list button exists");
WebDriverUtil.AssertElementNotPresent(item, By.ClassName("dropdown--square"), "Action button presented");
}
[Then(@"I verify not existing: ""(.*)"" button")]
public void ThenITrytoCheckButton(string p0)
{
ItemPageObject item = ScenarioContext.Current.Get<ItemPageObject>("item");
var action = item.SingleActionButton;
WebDriverUtil.AssertElementPresent(item, By.ClassName("btn__action"), "Action button presented");
var actionText = action.Text;
Assert.AreNotEqual(actionText, p0);
}
[Then(@"I verify not existing item ""(.*)"" from action list")]
public void WhenITryToPerformActionFromActionList(string p0)
{
var item = ScenarioContext.Current.Get<ItemPageObject>("item");
item.CheckForNotExsitingAction(_webDriver, item, "Action list not existed");
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.