microsoft / EasyRepro

Automated UI testing API for Dynamics 365
MIT License
513 stars 286 forks source link

Opens new window when running multiple tests #1228

Open reager opened 2 years ago

reager commented 2 years ago

Im using NUnit and latest preview of Easyrepro with chromedriver.

I've a setup were I created a baseclass which is responsible for logging in to my CRM instance. In my test class I have two method both annotated with [Test]-attributes and a method annotated with a [Setup] attribute. The SetUp method navigates to the start by opening the app and open a new entity form in create mode.

The problem is that after the first test has run to completion and the SetUp method is called again all windows are opened in a new tab instead of staying in the first one which fails all consecutive tests after the first one.

Is this a bug or is there any setting I should use?

Shakevg commented 2 years ago

@reager Could you please provide a code sample?

reager commented 2 years ago

Sure. This is my current setup

My base class that connects to my instance

    public class UIAutomationBase
    {
        private static readonly SecureString _username = ConfigurationManager.AppSettings["OnlineUsername"].ToSecureString();
        private static readonly SecureString _password = ConfigurationManager.AppSettings["OnlinePassword"].ToSecureString();
        private static readonly SecureString _mfaSecret = ConfigurationManager.AppSettings["MfaSecretKey"].ToSecureString();
        private static readonly Uri _xrmUri = new Uri(ConfigurationManager.AppSettings["OnlineCrmUrl"].ToString());

        protected static XrmApp XRMApp { get; }

        static UIAutomationBase()
        {
            var options = new BrowserOptions
            {
                BrowserType = BrowserType.Chrome,
                PrivateMode = true,
                Headless = false,
                UCITestMode = true,
                DriversPath = Path.GetDirectoryName(typeof(UIAutomationBase).Assembly.Location),
                StartMaximized = false,
                Height = 1080,
                Width = 1920
            };

            var client = new WebClient(options);

            XRMApp = new XrmApp(client);

            XRMApp.OnlineLogin.Login(_xrmUri, _username, _password, _mfaSecret);

        }

        [OneTimeTearDown]
        public void TearDown()
        {
            XRMApp.Dispose();
        }
    }

Some extensions to write tests more fluently

    public static class XrmAppEntityExtensions
    {
        public static Entity SetOptionSetValue(this Entity entity, string fieldName, int value)
        {
            entity.SetValue(new OptionSet
            {
                Name = fieldName,
                Value = value.ToString()
            });

            return entity;
        }

        public static Entity SetFieldValue(this Entity entity, string fieldName, string value)
        {
            entity.SetValue(fieldName, value);

            return entity;
        }

    }

and last my test cases

    [TestFixture]
    public class AccountTestCases : UIAutomationBase
    {
        [Test]
        public void AccountIsCreatedAsIndividualPersonWithAllRequiredFieldsButNoPersonalIdentityNumberEntered_AccountIsCreatedWithoutError()
        {
            XRMApp.Entity
                    .SetOptionSetValue("prefix_optionset1", 249160000)
                    .SetOptionSetValue("prefix_optionset2", 249160000)
                    .SetFieldValue("prefix_firstname", "Automatiserad")
                    .SetFieldValue("prefix_lastname", "Testperson")
                    .Save();
        }

        [Test]
        public void AccountIsCreatedAsCompanyWithAllRequiredFieldsButNoOrganizationNumberEntered_AccountIsCreatedWithoutError()
        {

            XRMApp.Entity
                    .SetOptionSetValue("prefix_optionset1", 249160001)
                    .SetOptionSetValue("prefix_optionset2", 249160000)
                    .SetFieldValue("name", "Automatiserat Testföretag")
                    .Save();
        }

        [SetUp]
        public void Setup()
        {
            XRMApp.Navigation.OpenSubArea("My sub area");

            XRMApp.CommandBar.ClickCommand("Skapa"); // Create
        }
    }

When I run my tests the first test that executes works as expected, but the second one opens all commands in new windows for some reason.

Shakevg commented 2 years ago

@reager I suppose constructor for UIAutomationBase executed for each test. Move code from constructor to method with [OneTimeSetUp] attribute

reager commented 2 years ago

@Shakevg It should only be called once, but after writing my last comment I did try OneTimeSetUp instead and it still open new windows.

ItsJ0el commented 2 years ago

@Shakevg I'm encountering the same Issue. On first execution of ClickElement it works fine, but every subsequent ClickElement opens it in a new tab. This causes the test to fail as it tries to search for the Element on the wrong tab. The testcase where ClickElement gets called once, succeeds, but all tests where it gets called more then once fails

Shakevg commented 2 years ago

Understand now. I saw one-time such an issue, but it was temporary. Looks it like something is wrong with the menu item. @reager Is it possible to reproduce for some standard menu items, not custom, to reproduce on my side? Can you try to click manually on the menu after the first test? What is NuGet version are you using, did you try wave2?

ItsJ0el commented 2 years ago

@Shakevg Sorry for the late reply. I used the latest preview version 9.2.21111.116-RW2-Preview. Manually clicking on the new tab sadly doesn't work, it continues running in the first tab. The Issue #1249 seems to be about the same problem.

Shakevg commented 2 years ago

@Shakevg Sorry for the late reply. Manually clicking on the new tab sadly doesn't work, it continues running in the first tab.

I mean in debug mode before the step that opens a new tab (I suppose it is OpenSubArea("My sub area") ) click on the link manually.

balakj75 commented 2 years ago

FWIW, the issue is with the Entity.Save method. This method uses the pattern of sending crtl S to the window to save the record. I think the browser thinks the ctrl key is still down after this method finishes executing and that is why the next click end up opening in a new tab. I switched to using the command bar save button and all is well.

alonsoatunicc commented 1 year ago

FWIW, the issue is with the Entity.Save method. This method uses the pattern of sending crtl S to the window to save the record. I think the browser thinks the ctrl key is still down after this method finishes executing and that is why the next click end up opening in a new tab. I switched to using the command bar save button and all is well.

You are completely right, the Ctrl key remains pressed. I've tried to use the ClickCommand("Save") instruction to use the command bar button but this is a menu button and I think I didn´t get the name correctly because it didn´t work for me.

What I did was updating the WebClient.cs file in the Microsoft.Dynamics365.UIAutomation.Api.UCI project as follows to release the Ctrl key:

internal BrowserCommandResult<bool> Save(int thinkTime = Constants.DefaultThinkTime)
{
    ThinkTime(thinkTime);

    return this.Execute(GetOptions($"Save"), driver =>
    {
        Actions action = new Actions(driver);
        action.KeyDown(Keys.Control).SendKeys("S").Perform();
        action.KeyUp(Keys.Control).Perform(); // Release the Ctrl key

        return true;
    });
}

I think this is a better solution as it will always work.

Regards.