Exemplo n.º 1
0
        public async Task Run_WhenThereAreInputElements_ThenItSuccessfullyTypesRelevantInformation()
        {
            using var driver = testFixture.GetWebDriver();

            var inputData = new Dictionary <string, string>
            {
                { "name", "Alex" },
                { "description", "Awesome Dev" },
                { "text", "This is a random text for me" }
            };

            try
            {
                var fileUri = new Uri(Path.GetFullPath($"{nameof(Run_WhenThereAreInputElements_ThenItSuccessfullyTypesRelevantInformation)}.html"));
                var random  = new Random(1);
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
                var seleniumTrainGoal = new SeleniumTrainGoal <IReadOnlyCollection <ElementData> >(async(_1, _2) =>
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
                {
                    var target = driver.FindElementByCssSelector(".third-panel");
                    return(target.Displayed && target.Enabled);
                });
                var seleniumEnvironment = new SeleniumEnvironment(
                    driver,
                    driver,
                    new SeleniumEnvironmentOptions
                {
                    Url           = fileUri.AbsoluteUri,
                    InputTextData = inputData,
                });
                var seleniumQLearningStepPolicy = new SeleniumQLearningStepPolicy(random);
                var rlTrainer = new RLTrainer <IReadOnlyCollection <ElementData> >(new RLTrainerOptions <IReadOnlyCollection <ElementData> >(seleniumEnvironment, seleniumQLearningStepPolicy, seleniumTrainGoal));

                await rlTrainer.Run(epochs : 5, maximumActions : 20);

                var initialState = await seleniumEnvironment.GetInitialState();

                var pathFinder = new RLPathFinder <IReadOnlyCollection <ElementData> >(seleniumEnvironment, seleniumQLearningStepPolicy);
                var pathList   = await pathFinder.FindRoute(initialState, seleniumTrainGoal);

                pathList.State.ShouldBe(PathFindResultState.GoalReached);
                pathList.Steps.ShouldNotBeNull();
                pathList.Steps.ShouldNotBeEmpty();
                pathList.Steps.Count.ShouldBe(5);
                pathList.Steps[0].Action.ToString().ShouldEndWith("input[data-automation-id='name']");
                pathList.Steps[1].Action.ToString().ShouldEndWith("textarea[data-automation-id='text']");
                pathList.Steps[2].Action.ToString().ShouldEndWith("input[data-automation-id='description']");
                pathList.Steps[3].Action.ToString().ShouldEndWith("input[data-automation-id='done']");
                pathList.Steps[4].Action.ToString().ShouldEndWith("input[data-automation-id='accept']");
            }
            finally
            {
                driver.Close();
                driver.Quit();
            }
        }
Exemplo n.º 2
0
        public async Task Run_WhenTrainingASimpleTestCase_ThenItSuccessfullyFindsTheCorrectActions()
        {
            using var driver = testFixture.GetWebDriver();

            try
            {
                var fileUri = new Uri(Path.GetFullPath($"{nameof(Run_WhenTrainingASimpleTestCase_ThenItSuccessfullyFindsTheCorrectActions)}.html"));
                var random  = new Random(1);
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
                var seleniumTrainGoal = new SeleniumTrainGoal <IReadOnlyCollection <ElementData> >(async(_1, _2) =>
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
                {
                    var target = driver.FindElementByCssSelector(".third");
                    return(target.Displayed && target.Enabled);
                });
                var seleniumEnvironment = new SeleniumEnvironment(
                    driver,
                    driver,
                    new SeleniumEnvironmentOptions
                {
                    Url = fileUri.AbsoluteUri,
                });
                var seleniumRandomStepPolicy = new SeleniumRandomStepPolicy(random);
                var rlTrainer = new RLTrainer <IReadOnlyCollection <ElementData> >(new RLTrainerOptions <IReadOnlyCollection <ElementData> >(seleniumEnvironment, seleniumRandomStepPolicy, seleniumTrainGoal));

                await rlTrainer.Run(epochs : 2, maximumActions : 20);

                var initialState = await seleniumEnvironment.GetInitialState();

                var pathFinder = new RLPathFinder <IReadOnlyCollection <ElementData> >(seleniumEnvironment, seleniumRandomStepPolicy);
                var pathList   = await pathFinder.FindRoute(initialState, seleniumTrainGoal);

                pathList.State.ShouldBe(PathFindResultState.GoalReached);
                pathList.Steps.ShouldNotBeNull();
                pathList.Steps.ShouldNotBeEmpty();
                pathList.Steps.Count.ShouldBe(3);
                pathList.Steps[0].Action.ToString().ShouldEndWith("input[data-automation-id='first']");
                pathList.Steps[1].Action.ToString().ShouldEndWith("input[data-automation-id='second']");
                pathList.Steps[2].Action.ToString().ShouldEndWith("button[data-automation-id='third']");
            }
            finally
            {
                driver.Close();
                driver.Quit();
            }
        }
Exemplo n.º 3
0
        public async Task Run_WhenTrainingWithPredefinedDemo_ThenItFindsTheCorrectSolution()
        {
            // Prepare
            var testEnvironment = new TestEnvironment();
            var testPolicy      = new TestPolicy();
            var trainGoal       = new TrainGoal();
            var rlTrainer       = new RLTrainer <int>(new RLTrainerOptions <int>(testEnvironment, testPolicy, trainGoal));

            // Execute
            await rlTrainer.Run(epochs : 50, maximumActions : 100);

            var pathFinder = new RLPathFinder <int>(testEnvironment, testPolicy);
            var result     = await pathFinder.FindRoute(new TestState(8), trainGoal);

            result.State.ShouldBe(PathFindResultState.GoalReached);
            result.Steps.ShouldNotBeNull();
            result.Steps.ShouldNotBeEmpty();
            result.Steps[0].ShouldBe(new StateAndActionPair <int>(new TestState(8), new TestAction(new TestState(9))));
            result.Steps[1].ShouldBe(new StateAndActionPair <int>(new TestState(9), new TestAction(new TestState(5))));
            result.Steps[2].ShouldBe(new StateAndActionPair <int>(new TestState(5), new TestAction(new TestState(6))));
            result.Steps[3].ShouldBe(new StateAndActionPair <int>(new TestState(6), new TestAction(new TestState(7))));
            result.Steps[4].ShouldBe(new StateAndActionPair <int>(new TestState(7), new TestAction(new TestState(11))));
        }