From c03bb95fb5bdadef3d50f787273caa8c2496daf4 Mon Sep 17 00:00:00 2001 From: Huaxing YUAN Date: Tue, 22 Jul 2025 14:51:22 +0200 Subject: [PATCH] Update chrome startup parameters Update example with test report --- .../AppFactory.cs | 6 +- src/AxaFrance.WebEngine.Web/BrowserFactory.cs | 6 +- .../Report/TestCaseReport.cs | 10 +++ .../Report/TestSuiteReport.cs | 2 + src/Samples.LinearScripting/UnitTest1.cs | 84 +++++++++++++++---- src/WebEngine.Test/UnitTests/AppMobileTest.cs | 1 + 6 files changed, 93 insertions(+), 16 deletions(-) diff --git a/src/AxaFrance.WebEngine.MobileApp/AppFactory.cs b/src/AxaFrance.WebEngine.MobileApp/AppFactory.cs index cd404ae..6c1daa5 100644 --- a/src/AxaFrance.WebEngine.MobileApp/AppFactory.cs +++ b/src/AxaFrance.WebEngine.MobileApp/AppFactory.cs @@ -1,6 +1,7 @@ // Copyright (c) 2016-2022 AXA France IARD / AXA France VIE. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. // Modified By: YUAN Huaxing, at: 2022-5-13 18:26 +using AngleSharp.Common; using Newtonsoft.Json.Linq; using OpenQA.Selenium.Appium; using OpenQA.Selenium.Appium.Android; @@ -281,7 +282,10 @@ private static AppiumDriver ConnectToDevice() //session not created (maybe a bug while there should be an exception) //convert driver.Capabilities of type ICapabilities to a string var cap = driver.Capabilities; - throw new WebEngineGeneralException("The driver has no session id. returning capabilities from grid:" + cap.ToString()); + string error = null; + if (cap.HasCapability("error")) error = cap.GetCapability("error").ToString(); + else if(cap.HasCapability("message")) error = cap.GetCapability("message").ToString(); + throw new WebEngineGeneralException("The driver has no session id. returning capabilities from grid:" + error?? cap.ToString()); } return driver; diff --git a/src/AxaFrance.WebEngine.Web/BrowserFactory.cs b/src/AxaFrance.WebEngine.Web/BrowserFactory.cs index 4808fac..1ca4692 100644 --- a/src/AxaFrance.WebEngine.Web/BrowserFactory.cs +++ b/src/AxaFrance.WebEngine.Web/BrowserFactory.cs @@ -10,6 +10,7 @@ using OpenQA.Selenium.Support.UI; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net; using System.Runtime.CompilerServices; @@ -443,7 +444,10 @@ private static WebDriver GetChromeDriver(IEnumerable browserOptions) { AcceptInsecureCertificates = true, }; - options.AddArgument($"user-data-dir={GetEnvironmentVariable("LOCALAPPDATA")}\\Google\\Chrome\\User Data"); + + string datadir = $"{GetEnvironmentVariable("LOCALAPPDATA")}\\Google\\Chrome\\TestData"; + Directory.CreateDirectory(datadir); + options.AddArgument($"user-data-dir={datadir}"); options.AddArgument("dns-prefetch-disable"); options.AddArgument("homepage=about:blank"); options.AddArgument("disable-popup-blocking"); diff --git a/src/AxaFrance.WebEngine/Report/TestCaseReport.cs b/src/AxaFrance.WebEngine/Report/TestCaseReport.cs index 1fed41a..b5ed217 100644 --- a/src/AxaFrance.WebEngine/Report/TestCaseReport.cs +++ b/src/AxaFrance.WebEngine/Report/TestCaseReport.cs @@ -16,6 +16,16 @@ namespace AxaFrance.WebEngine.Report public class TestCaseReport { + /// + /// Initializes a new instance of the class. + /// + public TestCaseReport() + { + StartTime = DateTime.Now; + EndTime = DateTime.Now; + Result = Result.None; + } + /// /// The unique identifier of the report item, generated automaticaly and used for showing report /// diff --git a/src/AxaFrance.WebEngine/Report/TestSuiteReport.cs b/src/AxaFrance.WebEngine/Report/TestSuiteReport.cs index b09790b..6d72d2e 100644 --- a/src/AxaFrance.WebEngine/Report/TestSuiteReport.cs +++ b/src/AxaFrance.WebEngine/Report/TestSuiteReport.cs @@ -97,6 +97,8 @@ public TestSuiteReport() { TestResult = new List(); EnvironmentVariables = new EnvironmentVariables(); + StartTime = DateTime.Now; + EndTime = DateTime.Now; } /// diff --git a/src/Samples.LinearScripting/UnitTest1.cs b/src/Samples.LinearScripting/UnitTest1.cs index b48a5ad..003989d 100644 --- a/src/Samples.LinearScripting/UnitTest1.cs +++ b/src/Samples.LinearScripting/UnitTest1.cs @@ -1,5 +1,7 @@ using AxaFrance.WebEngine; +using AxaFrance.WebEngine.Report; using AxaFrance.WebEngine.Web; +using OpenQA.Selenium; namespace Samples.LinearScripting @@ -8,42 +10,96 @@ namespace Samples.LinearScripting public class UnitTest1 { //WebDriver object will be used for each test case. - OpenQA.Selenium.WebDriver? driver = null; + WebDriver? driver = null; + TestCaseReport testCaseReport; + static TestSuiteReport? testSuiteReport = null; - [TestInitialize] - public void Setup() + public TestContext? TestContext { get; set; } + + [ClassInitialize] + public static void ClassInitialize(TestContext testContext) { - //Initialize the driver by platform and browser type - driver = AxaFrance.WebEngine.Web.BrowserFactory.GetDriver( - AxaFrance.WebEngine.Platform.Windows, - AxaFrance.WebEngine.BrowserType.Chrome); - driver.Navigate().GoToUrl("https://axafrance.github.io/webengine-dotnet/demo/Step1.html"); + testSuiteReport = new TestSuiteReport(); } + [TestInitialize] + public void TestInitialize() + { + //Initialize the test case report and add it to the test suite report. + testCaseReport = new TestCaseReport + { + TestName = TestContext!.TestName, + StartTime = DateTime.Now + }; + testSuiteReport!.TestResult.Add(testCaseReport); + //Initialize the driver by platform and browser type + driver = BrowserFactory.GetDriver(AxaFrance.WebEngine.Platform.Windows, BrowserType.Chrome); + } [TestCleanup] - public void Teardown() + public void TestCleanup() { + //After each test case, we will save the test case report. + testCaseReport.Result = TestContext!.CurrentTestOutcome switch + { + UnitTestOutcome.Passed => Result.Passed, + UnitTestOutcome.Failed => Result.Failed, + UnitTestOutcome.Inconclusive => Result.Ignored, + UnitTestOutcome.Error => Result.CriticalError, + _ => Result.None + }; + testCaseReport.EndTime = DateTime.Now; //Close the driver (and browser) after each test case. driver?.Close(); driver?.Dispose(); } + [ClassCleanup] + public static void ClassTearDown() + { + //After all test cases, we will save the test suite report. + testSuiteReport!.EndTime = DateTime.Now; + var reportPath = testSuiteReport.SaveAs("TestReport", "MyTestReport", false); + + //you can check the test report in folder bin\Debug\net8.0\TestReport of your test project. + DebugLogger.WriteLine($"Test Suite Report saved at: {reportPath}"); + } + /// + /// Example test method to select a drink and check the next page title. + /// [TestMethod] - public void TestMethod1() + public void SelectDrinkTest() { - driver = BrowserFactory.GetDriver(Platform.Windows, BrowserType.Chrome); - driver.Navigate().GoToUrl("https://axafrance.github.io/webengine-dotnet/demo/Step1.html"); - DrinkSelectionPageModel page = new DrinkSelectionPageModel(driver); + driver!.Navigate().GoToUrl("https://axafrance.github.io/webengine-dotnet/demo/Step1.html"); + + + //during the test execution, we can add some action reports to the test case report. + testCaseReport.ActionReports.Add(new ActionReport + { + Name = "Navigate to Step 1 page", + Result = Result.Passed, + Log = "Navigated to Step 1 page successfully." + }); + //Create a page model for the Drink Selection page. + DrinkSelectionPageModel page = new DrinkSelectionPageModel(driver); page.SelectLanguage.SelectByValue("fr"); page.RadioChooseToBuy.CheckByValue("Coffee"); page.NextButton.Click(); - Assert.IsTrue(page.Page2Title.Exists()); + testCaseReport.ActionReports.Add(new ActionReport + { + Name = "Select language and drink", + Result = Result.Passed, + Log = "Selected French language and Coffee drink." + }); + testCaseReport.Log = "This is the log of curren test case."; + + //Check if the next page is loaded correctly. + Assert.IsTrue(page.Page2Title.Exists()); } } } \ No newline at end of file diff --git a/src/WebEngine.Test/UnitTests/AppMobileTest.cs b/src/WebEngine.Test/UnitTests/AppMobileTest.cs index 468b98a..2effe4c 100644 --- a/src/WebEngine.Test/UnitTests/AppMobileTest.cs +++ b/src/WebEngine.Test/UnitTests/AppMobileTest.cs @@ -45,6 +45,7 @@ public void IntegrationTest() System.IO.File.Delete(junitReport); } + [TestMethod] public void UnitTest() {