I am writing a UI test. This is to check for the error 404 page which I have enabled in Web.Config via;
<customErrors mode="On" redirect="~/Errors/"/>
This all works fine, however I only have custom errors set to "On" whilst in the "UAT" development environment. If I am in "Dev" or "IST" then I still want to see the default ASP.Net errors.
So now back to the UI test using Selenium
public string GetAlertBoxDetails()
{
IWebElement alertBox = _driver.FindElement(By.CssSelector(".alert.alert-danger"));
return alertBox.Text;
}
As you can see I am detecting the Bootstrap ".alert.alert-danger" box and returning the text inside. I then check to see if this text contains "Sorry, that page doesn't exist.". I am using Specflow for the text story.
[Then(#"The user should be told that no such page exists")]
public void ThenTheUserShouldBeToldThatNoSuchPageExists()
{
string alertboxDetail = GetAlertBoxDetails();
Assert.IsTrue(alertboxDetail.Contains("Sorry, that page doesn't exist."), "Couldn't find the message \"Sorry, that page doesn't exist.\"");
}
This all works fine, however I would only like this test to run in the UAT environment. This is because the element ".alert.alert-danger" will only be found if customErrors is set to "Off". For this I have included this step in the test.
[Given(#"I am in the UAT environment")]
public void GivenIAmInTheUATEnvironment()
{
var env = EnvironmentType;
if (env != EnvironmentType.Uat)
{
Assert.Inconclusive($"Cannot run this test on environment: {env}. " +
$"This test is only for the UAT environment.");
}
else
{
Assert.IsTrue(true);
}
}
Again this works fine. My only problem is that I do not want to use "Assert.Inconclusive" I would rather "Assert.Pass" and say the test passed if it is carried out in a non-UAT environment.
I see XUnit has an Assert.Pass function, but can this be done in MsTest? To force a test to pass WITHOUT continuing to the next Assert. In specflow I am running the "given" step I would like to stop it from continuing to the "Then" step.
WRT NUnit, you can try Assert.Pass. I can't try it out myself right now as I'm traveling. My uncertainty is that I'm not sure if it will prevent the test from being run if you do it in the SetUp, which is what Given maps to.
My view is that accepting the behavior you looking for, all the code belongs in the test itself and not the Given. What Given would normally do would be to actually create the situation you expect, i.e. change the environment. That's obviously not possible here so I would simply put the environment check in the test itself. I wouldn't even use Assert.Pass unless you want a special message, I'd just skip the test code if the environment is wrong. As a side benefit, this approach works for all three test frameworks.
Although you didn't ask, I have to say that the instructions you have been given to show the test as passing even if it is not run seem pretty crazy to me!
Related
I am using Actions class to move to an element and then to click on it.
The following code works when I execute it on debug mode, but the same fails during normal test execution. In my case, State is a dropdown and I am trying to select a state from a dropdown.
There was a similar question and I used all options mentioned over there, but still couldn't get pass it.
Code trials:
Driver.WaitForPageLoad();
Driver.WaitForAjax();
BrowserActions action = new BrowserActions(Driver);
action.MoveToElement(Driver.FindElement(State().Query)).Build().Perform();
Driver.WaitUntil(ExpectedConditions.ElementToBeClickable(State().Query), 120);
action.Click().Perform();
Driver.WaitForAjax();
Driver.WaitForPageLoad();
Can someone clarify?
From a generic approach, MoveToElement() would be successfull only when ExpectedConditions.ElementToBeClickable() or atleast ExpectedConditions.ElementIsVisible() succeeds.
So you can optimize your code block as follows:
Driver.WaitForPageLoad();
Driver.WaitForAjax();
BrowserActions action = new BrowserActions(Driver);
action.MoveToElement(Driver.WaitUntil(ExpectedConditions.ElementToBeClickable(State().Query), 120)).Click().Build().Perform();
Apologies if this is a long question, it's causing us confusion and is stopping us progressing with a project.
The company I work at had a successful working automated test pack using Page Object Models with C# Selenium, for one of our products.
The product in question was coded with .Net Framework.
The product has since been moved to .Net Core 3 with Blazor. The test itself is also being written in .Net Core 3
Even with a re-write this has resulted in the test pack becoming incredibly unreliable.
For example, a test to update a user's personal details updates the text boxes, saves it, asserts that the changes have been made and puts the data back to the previous values.
These are using straight forward methods and will only run reliably the first time.
A second run will fail to press the update button.
The third run will fail to clear the text boxes (even with a clear command). It places the new data in the text box at the end of the existing data. On one text box if fails to do anything.
The puzzling thing is that the tests will report as passing on the 1st and 2nd runs.
Example methods I've written that are as follows:
public void InputProfile(string PersonalEmail, string MobilePhone, string VehicleReg)
{
//update personal email
Page.Profile.PersonalEmail.Click();
Page.Profile.PersonalEmail.Clear();
Page.Profile.PersonalEmail.SendKeys(PersonalEmail);
//update mobile phone
Page.Profile.MobilePhone.Click();
Page.Profile.MobilePhone.Clear();
Page.Profile.MobilePhone.SendKeys(MobilePhone);
//update vehicle registration
Page.Profile.VehicleRegistration.Click();
Page.Profile.VehicleRegistration.Clear();
Page.Profile.VehicleRegistration.SendKeys(VehicleReg);
}
public void UpdateProfile()
{
//pressing the update button go back to the main page
Page.Profile.UpdateButton.Click();
Hooks.WebDriver.Navigate().GoToUrl("https://XXXX/");
Hooks.Wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.PresenceOfAllElementsLocatedBy(By.Id("ContactLink")));
}
public void AssertProfile(string PersonalEmail, string MobilePhone, string VehicleReg)
{
//Get the current text from the boxes
string AssertEmail = Page.Profile.PersonalEmail.GetAttribute("value").ToString();
string AssertMobile = Page.Profile.MobilePhone.GetAttribute("value").ToString();
string AssertVehicleReg = Page.Profile.VehicleRegistration.GetAttribute("value").ToString();
//Check the text with the expected values
Assert.AreEqual(PersonalEmail, AssertEmail);
Assert.AreEqual(MobilePhone, AssertMobile);
Assert.AreEqual(VehicleReg, AssertVehicleReg);
}
We're using the following list of Nuget packages, I suspect something's wrong here. With the exception of the DotNetSeliniumExtras.PageObjects.Core they're the same as we've used in the .Net Framework version of the test.
Microsoft.NET.Test.Sdk
Selenium.WebDriver
Selenium.Support
Selenium.WebDriver.ChromeDriver
Selenium.WebDriver.IEDriver
Selenium.Firefox.WebDriver
DotNetSeliniumExtras.WaitHelper
DotNetSeliniumExtras.PageObjects.Core
NUnit
NUnit3TestAdapter
I'm working on a Product automation(Web CMS), where element.Click() shows the inconsistent behaviour. Basically we are using,
Selenium + Nunit GUI(unit testing framework) - To run the test cases from local on a particular environment
Selenium + Asp.net web application - Multiple user's can run the test cases on different environment
Here environment I mean different levels(Dev, SIT, QA, Production).
My Concern
In one of my test cases, I want to Click a button. So for that, I have tried few code. But all are inconsistent behaviour. Here Inconsistent I mean, the code whatever I wrote for clicking a button are only working in my local or server and viceversa.
1st attempt:-
I tried all the element locator's
IWebElement element = driver.FindElement(By.Id("element id goes here"))
Working fine at my local, but not in server
Result - Failed
2nd attempt:-
driver.FindElement(By.XPath("Element XPath goes here")).SendKeys(Keys.Enter);
Working fine at server, but not in local
Result - Failed
3rd attempt:-
IWebElement element = driver.findElement(By.id("something"));
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].click()", element);
Not working in both(local and server)
Result - Failed
At last, I tried waiting for the element to be visible and performing action
4th attempt:-
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
return wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("element xpath goes here")));
After webdriver wait performing action on that element (element.click())
Working fine at local but not in server
Result - Failed
I'm looking for a solution, where Clicking the button should not be an inconsistent behaviour. Basically it should work fine in both (Local and Server). Your help would be greatly appreciated..Thanks in advance
FYI - I'm testing in Mozilla Firefox browser 38.5.2
I'm using Selenium in C# locally on Win7 and remotely on Win10 and MacOS with the Firefox browser and also noticed that Firefox sometimes requires special treatment for IWebElement.Click(). So I wrote myself an extension method, which works fine for me, no matter by what locator the element was found:
public static void Click(this IWebElement element, TestTarget target)
{
if (target.IsFirefox)
{
var actions = new Actions(target.Driver);
actions.MoveToElement(element);
// special workaround for the FirefoxDriver
// otherwise sometimes Exception: "Cannot press more then one button or an already pressed button"
target.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.Zero);
// temporarily disable implicit wait
actions.Release().Build().Perform();
target.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(MyDefaultTimeoutInSeconds));
Thread.Sleep(500);
actions.MoveToElement(element);
actions.Click().Build().Perform();
}
else
{
element.Click();
}
}
If you want more stable behavior for your test cases, I would recommend using ChromeDriver. It never needs any special treatment at all, and it's also much faster than the FirefoxDriver.
With Team Foundation Server, given a WorkItem of type "Test Suite," how can I write a query to select all Test Cases associated to that Test Suite?
Unfortunately, there are no work item links created between Test Plans, Suites and Cases. So although they are Work Items, they don't have links. This means that a default query isn't possible.
A work around is tagging all test cases in a suite with the name of the suite. You can then use a query that filters on the work item tags.
You can go even further and automate the creation of tags by using some Web Hooks and Azure Functions (or some other hosted API) magic. This allows you to create a Web Hook that listens for the creation (or updates) to Test Cases. By using some of the code mentioned in the other posts you can retrieve the Test Suite of the Test Case and then use the REST API to add it as a Tag to the Test Case.
You may need to use this Interface ITestSuiteBase.
AllTestCases
Gets the read-only collection of test cases for this suite and all hierarchical children.
TestCases
Gets a read-only collection of test cases.
More info from MSDN
Here is a example code:
public static List<TestCase> GetAllTestCaseFromSuite(ITestPlan testPlan, int suiteId, bool includeExecutionStatus = true)
{
List<TestCase> testCases = new List<TestCase>();
testPlan.Refresh();
ITestSuiteBase currentSuite = testPlan.Project.TestSuites.Find(suiteId);
currentSuite.Refresh();
foreach (var currentTestCase in currentSuite.TestCases)
{
TestCase testCaseToAdd = new TestCase(currentTestCase.TestCase, currentSuite, testPlan, includeExecutionStatus);
if (!testCases.Contains(testCaseToAdd))
{
testCases.Add(testCaseToAdd);
}
}
log.InfoFormat("Load all test cases in the suite with Title= \"{0}\" id = \"{1}\"", currentSuite.Title, currentSuite.Id);
return testCases;
}
More details you can refer this blog: Manage TFS Test Cases C# Code
If you are using TFS 2015 or higher,
you can check this link :
usage
TestCaseExplorer Tool
list-bugs-and-the-test-cases-that-test-them
if Not using TFS 2015 or higher :
For now, there is no way to create ordinary TFS query via Web interface and not API call or custom coding to get list of Test Cases belongs to a specific Test Suite. support-querying-for-all-test-cases-in-a-specifed
Or try old tool : test-plans-test-suites-test-cases-mapping
"Show tests from child suites" is the option that you want. To see the screenshot click here. No need for the query.
As the name of the option says it lists all the child tests from the suite. You might need Test Manager TFS plugin for this.
I am trying to create a UI test in VS 2010 using IE 9 in IE 8 compatibilty mode however when trying to record an action recording many of the steps fail. Then when I manually code in the missing steps and try to fill in a log in form with a username and password I get an exception that says I have failed to perform an action on hidden control.
The UI Test code:
public void Recordedmethod()
{
BrowserWindow uILogInWindowsInternetWindow = this.UILogInWindowsInternetWindow;
HtmlHyperlink uILogInHyperlink = this.UILogInWindowsInternetWindow.UIHomePageDocument.UILogInHyperlink;
HtmlEdit uIUsernameEdit = this.UILogInWindowsInternetWindow.UILogInDocument1.UIUsernameEdit;
HtmlEdit uIPasswordEdit = this.UILogInWindowsInternetWindow.UILogInDocument1.UIPasswordEdit;
#endregion
// Go to web page 'http://localhost:15856/WebSite1/'
uILogInWindowsInternetWindow.NavigateToUrl(new System.Uri(this.RecordedMethodParams.UILogInWindowsInternetWindowUrl));
// Set flag to allow play back to continue if non-essential actions fail. (For example, if a mouse hover action fails.)
Playback.PlaybackSettings.ContinueOnError = true;
// Mouse hover 'Log In' link at (1, 1)
Mouse.Click(uILogInHyperlink);
// Reset flag to ensure that play back stops if there is an error.
Playback.PlaybackSettings.ContinueOnError = false;
// Type 'test' in 'Username:' text box
uIUsernameEdit.Text = this.RecordedMethodParams.UIUsernameEditText;
// The following element is no longer available: IE web control; Process Id [6320], window handle [3168166]
// Type '********' in 'Password:' text box
uIPasswordEdit.Password = this.RecordedMethodParams.UIPasswordEditPassword;
// The following element is no longer available: IE web control; Process Id [6320], window handle [3168166]
}
This is an issue linked to an Internet Explorer patch that was released in September.
KB2870699
This affects VS2010 and VS2012.
Microsoft released a patch that corrects the issue for VS2012 (and I've confirmed that it fixed the issue for me).
http://blogs.msdn.com/b/visualstudioalm/archive/2013/09/17/coded-ui-mtm-issues-on-internet-explorer-with-kb2870699.aspx
Currently the only workaround for VS2010 is to uninstall the patch (KB2870699); however, as with any sort of security patch you'll want to consider carefully whether pulling it is safe to do given your situation.
EDIT: This was not a fun bug for me to deal with. I had just upgraded to VS2012 from VS2010 and all of a sudden I found none of my previously functioning CodedUI tests working. I assumed it was an issue with VS2012 and after banging my head against the wall for the better part of a day I found out it was an issue with a patch. It was just my luck that I upgraded to 2012 at the same time the patch had been installed on my system. Good times!
There is actually an updated for VS 2012 to fix this issue
http://blogs.msdn.com/b/visualstudioalm/archive/2013/09/17/coded-ui-mtm-issues-on-internet-explorer-with-kb2870699.aspx
Hope this helps!
I was having the same problem with my coded ui test. It's an issue with VS-2012 i guess, i tried every update (installing/uninstalling them and everything..) nothing worked.
I tried VS-2013 Ultimate and it worked.
You can use exception handling to capture the error while still not having the test failed.
The test is failing because at the time it performs click action, the control is hidden.
try
{
//your code goes here
}
catch(FailedToPerformActionOnHiddenControlException e)
{
Console.WriteLine(e.Message);
}