From 815722ae1b5dabd77478bbd9705e306a82b94ad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20=C5=A0est=C3=A1k?= Date: Sun, 2 Jun 2024 23:03:30 +0100 Subject: [PATCH 01/11] Added BrowserVersion support --- .github/workflows/main.yml | 17 ++++++ .../Drivers/Implementation/ChromeHelpers.cs | 10 ++-- .../CoordinatorWebBrowserBase.cs | 1 - .../Implementation/DictionaryExtensions.cs | 17 ++++++ .../Drivers/Implementation/EdgeHelpers.cs | 1 + .../Drivers/Implementation/FirefoxHelpers.cs | 59 ++----------------- .../Implementation/InternetExplorerHelpers.cs | 3 +- .../Riganti.Selenium.DotVVM3.Samples.csproj | 2 +- .../seleniumconfig.json | 3 +- .../BrowsersStabilityUiTests.cs | 2 +- .../seleniumconfig.json | 3 +- .../seleniumconfig.json | 4 +- 12 files changed, 55 insertions(+), 67 deletions(-) create mode 100644 src/Core/Riganti.Selenium.Core/Drivers/Implementation/DictionaryExtensions.cs diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 84335c11..6d8cff2d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -58,3 +58,20 @@ jobs: github-token: ${{ secrets.GITHUB_TOKEN }} build-configuration: "${{ matrix.environment == 'Production' && 'Release' || 'Debug' }}" runtime-environment: "${{ matrix.environment }}" + report: + runs-on: ubuntu-latest + steps: + - name: Test Reporter + uses: dorny/test-reporter@v1.9.1 + with: + artifact: chrome-tests-Linux--Default # artifact name + name: Chrome UI Tests # Name of the check run which will be created + path: '*.trx' # Path to test results (inside artifact .zip) + reporter: dotnet-trx # Format of test results + - name: Test Reporter + uses: dorny/test-reporter@v1.9.1 + with: + artifact: firefox-tests-Linux--Default # artifact name + name: Firefox UI Tests # Name of the check run which will be created + path: '*.trx' # Path to test results (inside artifact .zip) + reporter: dotnet-trx # Format of test results diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/ChromeHelpers.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/ChromeHelpers.cs index ba753a3f..684becef 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/ChromeHelpers.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/ChromeHelpers.cs @@ -1,19 +1,22 @@ -using OpenQA.Selenium.Chrome; +using OpenQA.Selenium; +using OpenQA.Selenium.Chrome; using Riganti.Selenium.Core.Factories; +using System.Collections.Generic; +using System.Runtime.CompilerServices; namespace Riganti.Selenium.Core.Drivers.Implementation { public static class ChromeHelpers { - public static ChromeDriver CreateChromeDriver(LocalWebBrowserFactory factory) { - + var options = new ChromeOptions(); options.AddArgument("test-type"); options.AddArgument("disable-popup-blocking"); options.AddArguments(factory.Capabilities); + options.BrowserVersion = factory.Options.TryGet(nameof(options.BrowserVersion)); if (factory.GetBooleanOption("disableExtensions")) { @@ -21,6 +24,5 @@ public static ChromeDriver CreateChromeDriver(LocalWebBrowserFactory factory) } return new ChromeDriver(options); } - } } \ No newline at end of file diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/CoordinatorWebBrowserBase.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/CoordinatorWebBrowserBase.cs index 87bc0910..568fa15c 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/CoordinatorWebBrowserBase.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/CoordinatorWebBrowserBase.cs @@ -13,6 +13,5 @@ public CoordinatorWebBrowserBase(CoordinatorWebBrowserFactoryBase factory, Conta { Lease = lease; } - } } \ No newline at end of file diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/DictionaryExtensions.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/DictionaryExtensions.cs new file mode 100644 index 00000000..0e315754 --- /dev/null +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/DictionaryExtensions.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; + +namespace Riganti.Selenium.Core.Drivers.Implementation +{ + public static class DictionaryExtensions + { + + public static T2 TryGet(this IDictionary dic, T1 key) + { + if(dic is not null && dic.TryGetValue(key, out T2 value) && value is T2) + { + return value; + } + return default; + } + } +} \ No newline at end of file diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/EdgeHelpers.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/EdgeHelpers.cs index 82556080..e53a1fc1 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/EdgeHelpers.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/EdgeHelpers.cs @@ -11,6 +11,7 @@ public static EdgeDriver CreateEdgeDriver(LocalWebBrowserFactory factory) { }; + options.BrowserVersion = factory.Options.TryGet(nameof(options.BrowserVersion)); return new EdgeDriver(options); } } diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs index 8bea3836..ffc7d0fa 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs @@ -7,68 +7,16 @@ namespace Riganti.Selenium.Core.Drivers.Implementation { public static class FirefoxHelpers { - private static string pathToFirefoxBinary; - private static FirefoxDriverService service; - + static FirefoxHelpers() { service = FirefoxDriverService.CreateDefaultService(); - // service.LogLevel = FirefoxDriverLogLevel.Trace; } public static FirefoxDriver CreateFirefoxDriver(LocalWebBrowserFactory factory) { - factory.LogInfo($"Creating firefox driver from '{pathToFirefoxBinary}'."); - - if (!string.IsNullOrWhiteSpace(pathToFirefoxBinary)) - { - return CreateAlternativeInstance(); - } - - try - { - return new FirefoxDriver(service, GetFirefoxOptions()); - } - catch (Exception e) - { - factory.LogInfo("Firefox driver could not be created."); - factory.LogError(e); - - var env = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); - if (env.Contains("(x86)")) - { - env = env.Replace("(x86)", "").Trim(); - } - var firefox = "Mozilla Firefox\\Firefox.exe"; - if (File.Exists(Path.Combine(env, firefox))) - { - return CreateAlternativeInstance(env, firefox); - } - - env = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); - if (File.Exists(Path.Combine(env, firefox))) - { - return CreateAlternativeInstance(env, firefox); - } - throw; - } - } - - private static FirefoxDriver CreateAlternativeInstance(string env, string firefox) - { - pathToFirefoxBinary = Path.Combine(env, firefox); - return CreateAlternativeInstance(); - } - - private static FirefoxDriver CreateAlternativeInstance() - { - var firefoxOptions = new FirefoxOptions() - { - BrowserExecutableLocation = pathToFirefoxBinary, - Profile = GetFirefoxProfile() - }; - return new FirefoxDriver(firefoxOptions); + return new FirefoxDriver(service, GetFirefoxOptions(factory.Options)); } public static FirefoxProfile GetFirefoxProfile() @@ -80,9 +28,10 @@ public static FirefoxProfile GetFirefoxProfile() return profile; } - public static FirefoxOptions GetFirefoxOptions() + public static FirefoxOptions GetFirefoxOptions(System.Collections.Generic.IDictionary _options) { var options = new FirefoxOptions { Profile = GetFirefoxProfile() }; + options.BrowserVersion = _options.TryGet(nameof(options.BrowserVersion)); return options; } } diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/InternetExplorerHelpers.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/InternetExplorerHelpers.cs index 2e28cfd0..d9a0fc21 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/InternetExplorerHelpers.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/InternetExplorerHelpers.cs @@ -12,9 +12,8 @@ public static InternetExplorerDriver CreateInternetExplorerDriver(LocalWebBrowse { BrowserCommandLineArguments = "-private" }; - + options.BrowserVersion = factory.Options.TryGet(nameof(options.BrowserVersion)); return new InternetExplorerDriver(options); } - } } \ No newline at end of file diff --git a/src/Riganti.Selenium.DotVVM3Samples/Riganti.Selenium.DotVVM3.Samples.csproj b/src/Riganti.Selenium.DotVVM3Samples/Riganti.Selenium.DotVVM3.Samples.csproj index f08253e5..0cde2ce2 100644 --- a/src/Riganti.Selenium.DotVVM3Samples/Riganti.Selenium.DotVVM3.Samples.csproj +++ b/src/Riganti.Selenium.DotVVM3Samples/Riganti.Selenium.DotVVM3.Samples.csproj @@ -1,6 +1,6 @@  - net5.0 + net8.0 diff --git a/src/Tests/Riganti.Selenium.Core.Samples.Assert.Tests/seleniumconfig.json b/src/Tests/Riganti.Selenium.Core.Samples.Assert.Tests/seleniumconfig.json index d247e3cd..1c93c9af 100644 --- a/src/Tests/Riganti.Selenium.Core.Samples.Assert.Tests/seleniumconfig.json +++ b/src/Tests/Riganti.Selenium.Core.Samples.Assert.Tests/seleniumconfig.json @@ -1,7 +1,8 @@ { "factories": { "chrome:fast": { - "capabilities": [ "--window-size=1920,1080" ]//, "--headless", "--disable-gpu" ] + "capabilities": [ "--window-size=1920,1080" , "--headless", "--disable-gpu" ], + "BrowserVersion": "124" } //"firefox:dev": { //} diff --git a/src/Tests/Riganti.Selenium.Core.Samples.Tests/BrowsersStabilityUiTests.cs b/src/Tests/Riganti.Selenium.Core.Samples.Tests/BrowsersStabilityUiTests.cs index a5448bc0..de9ed215 100644 --- a/src/Tests/Riganti.Selenium.Core.Samples.Tests/BrowsersStabilityUiTests.cs +++ b/src/Tests/Riganti.Selenium.Core.Samples.Tests/BrowsersStabilityUiTests.cs @@ -70,7 +70,7 @@ public void SelectMethod_XPathToRoot_ExpectedException() } [TestMethod] - [ExpectedSeleniumException(typeof(InvalidSelectorException))] + [ExpectedSeleniumException(typeof(JavaScriptException))] public void SelectMethod_InvalidXPathSelector_ExpectedException() { this.RunInAllBrowsers(browser => diff --git a/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json b/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json index d1b32b0f..c75902e1 100644 --- a/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json +++ b/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json @@ -1,7 +1,8 @@ { "factories": { "chrome:fast": { - "capabilities": [ "--window-size=1920,1080", "--headless", "--disable-gpu" ] + "capabilities": [ "--window-size=1920,1080", "--headless", "--disable-gpu" ], + "BrowserVersion": "124" } //"chrome:coordinator": { // "options": { diff --git a/src/Tests/Riganti.Selenium.Sandbox/seleniumconfig.json b/src/Tests/Riganti.Selenium.Sandbox/seleniumconfig.json index 085d8bb1..f44678e6 100644 --- a/src/Tests/Riganti.Selenium.Sandbox/seleniumconfig.json +++ b/src/Tests/Riganti.Selenium.Sandbox/seleniumconfig.json @@ -1,6 +1,8 @@ { "factories": { - "firefox:fast": {} + "firefox:fast": { + "BrowserVersion": "stable" + } }, "baseUrls": [ "http://localhost:6278/" From 844baf2b72059a2c950c4e222ff3fbbbdad676ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20=C5=A0est=C3=A1k?= Date: Sun, 2 Jun 2024 23:27:45 +0100 Subject: [PATCH 02/11] Fixed Firefox Cookies issue --- .../Drivers/FastWebBrowserBase.cs | 25 +++++++++++++++++++ .../FunctionsUiTests.cs | 2 +- .../seleniumconfig.json | 10 +++++--- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/Core/Riganti.Selenium.Core/Drivers/FastWebBrowserBase.cs b/src/Core/Riganti.Selenium.Core/Drivers/FastWebBrowserBase.cs index d296b72a..d6bbbfc3 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/FastWebBrowserBase.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/FastWebBrowserBase.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Reflection; using OpenQA.Selenium; +using OpenQA.Selenium.Support.Extensions; using Riganti.Selenium.Core.Factories; namespace Riganti.Selenium.Core.Drivers @@ -67,7 +68,31 @@ protected virtual void CleanSessionAndLocalStorage() /// protected virtual void DeleteAllCookies() { + driverInstance.Manage().Cookies.DeleteAllCookies(); + + // Firefox driver doesn't list all cookies in Manage().Cookies - therefore JS invocation to remove all cookies + // Copied from https://stackoverflow.com/a/33366171 + driverInstance.ExecuteJavaScript( + @" +(function () { + var cookies = document.cookie.split(""; ""); + for (var c = 0; c < cookies.length; c++) { + var d = window.location.hostname.split("".""); + while (d.length > 0) { + var cookieBase = encodeURIComponent(cookies[c].split("";"")[0].split(""="")[0]) + '=; expires=Thu, 01-Jan-1970 00:00:01 GMT; domain=' + d.join('.') + ' ;path='; + var p = location.pathname.split('/'); + document.cookie = cookieBase + '/'; + while (p.length > 0) { + document.cookie = cookieBase + p.join('/'); + p.pop(); + }; + d.shift(); + } + } +})(); +" + ); } /// diff --git a/src/Tests/Riganti.Selenium.Core.Samples.Tests/FunctionsUiTests.cs b/src/Tests/Riganti.Selenium.Core.Samples.Tests/FunctionsUiTests.cs index 5a304fb4..c8a7d4ee 100644 --- a/src/Tests/Riganti.Selenium.Core.Samples.Tests/FunctionsUiTests.cs +++ b/src/Tests/Riganti.Selenium.Core.Samples.Tests/FunctionsUiTests.cs @@ -253,7 +253,7 @@ public void CookieTest() browser.NavigateToUrl("/test/Cookies"); browser.First("#CookieIndicator").CheckIfInnerTextEquals("default value"); - browser.Click("#SetCookies").Wait(); + browser.Click("#SetCookies").Wait(1000); browser.NavigateToUrl("/test/Cookies"); browser.First("#CookieIndicator").CheckIfInnerTextEquals("new value"); }; diff --git a/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json b/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json index c75902e1..68028147 100644 --- a/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json +++ b/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json @@ -1,8 +1,12 @@ { "factories": { - "chrome:fast": { - "capabilities": [ "--window-size=1920,1080", "--headless", "--disable-gpu" ], - "BrowserVersion": "124" + //"chrome:fast": { + // "capabilities": [ "--window-size=1920,1080", "--headless", "--disable-gpu" ], + // "BrowserVersion": "124" + //}, + "firefox:fast": { + //"capabilities": [ "--window-size=1920,1080", "--headless", "--disable-gpu" ], + "BrowserVersion": "stable" } //"chrome:coordinator": { // "options": { From 0d4d8e898de1547a1ad22bdda8f5fb53c72c3e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20=C5=A0est=C3=A1k?= Date: Sun, 2 Jun 2024 23:29:03 +0100 Subject: [PATCH 03/11] Reverted default chrome driver settings --- .../seleniumconfig.json | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json b/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json index 68028147..57398adc 100644 --- a/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json +++ b/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json @@ -1,13 +1,13 @@ { "factories": { - //"chrome:fast": { + "chrome:fast": { + "capabilities": [ "--window-size=1920,1080", "--headless", "--disable-gpu" ], + "BrowserVersion": "124" + }, + //"firefox:fast": { // "capabilities": [ "--window-size=1920,1080", "--headless", "--disable-gpu" ], - // "BrowserVersion": "124" - //}, - "firefox:fast": { - //"capabilities": [ "--window-size=1920,1080", "--headless", "--disable-gpu" ], - "BrowserVersion": "stable" - } + // "BrowserVersion": "stable" + //} //"chrome:coordinator": { // "options": { // "coordinatorUrl": "http://localhost:62242/" From abec6671e35d983c2db6292566fcdfd3f05758e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20=C5=A0est=C3=A1k?= Date: Sun, 2 Jun 2024 23:38:40 +0100 Subject: [PATCH 04/11] Version changed for publishing, reporter testing --- .github/workflows/main.yml | 24 +++++++----------------- src/Directory.Build.props | 2 +- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6d8cff2d..53a31bac 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -58,20 +58,10 @@ jobs: github-token: ${{ secrets.GITHUB_TOKEN }} build-configuration: "${{ matrix.environment == 'Production' && 'Release' || 'Debug' }}" runtime-environment: "${{ matrix.environment }}" - report: - runs-on: ubuntu-latest - steps: - - name: Test Reporter - uses: dorny/test-reporter@v1.9.1 - with: - artifact: chrome-tests-Linux--Default # artifact name - name: Chrome UI Tests # Name of the check run which will be created - path: '*.trx' # Path to test results (inside artifact .zip) - reporter: dotnet-trx # Format of test results - - name: Test Reporter - uses: dorny/test-reporter@v1.9.1 - with: - artifact: firefox-tests-Linux--Default # artifact name - name: Firefox UI Tests # Name of the check run which will be created - path: '*.trx' # Path to test results (inside artifact .zip) - reporter: dotnet-trx # Format of test results + - name: Test Reporter + uses: dorny/test-reporter@v1.9.1 + with: + artifact: ${{ matrix.browser }}-tests-Linux--Default # artifact name + name: ${{ matrix.browser }} UI Tests # Name of the check run which will be created + path: '*.trx' # Path to test results (inside artifact .zip) + reporter: dotnet-trx # Format of test results diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 7a7f4acd..07fa113a 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -4,7 +4,7 @@ RIGANTI Basic utilities for performing UI tests based on selenium on CI servers. selenium;ui;tests;riganti;ci - 3.0.0-preview15-final + 3.0.0-preview16-browser-manager Icon.png git https://github.com/riganti/selenium-utils From c38ac3281987b870367c6785b4df197f501d02cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20=C5=A0est=C3=A1k?= Date: Sun, 2 Jun 2024 23:45:52 +0100 Subject: [PATCH 05/11] SelectMethod_InvalidXPathSelector_ExpectedException fixed --- .../BrowsersStabilityUiTests.cs | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Tests/Riganti.Selenium.Core.Samples.Tests/BrowsersStabilityUiTests.cs b/src/Tests/Riganti.Selenium.Core.Samples.Tests/BrowsersStabilityUiTests.cs index de9ed215..eac552b2 100644 --- a/src/Tests/Riganti.Selenium.Core.Samples.Tests/BrowsersStabilityUiTests.cs +++ b/src/Tests/Riganti.Selenium.Core.Samples.Tests/BrowsersStabilityUiTests.cs @@ -70,15 +70,23 @@ public void SelectMethod_XPathToRoot_ExpectedException() } [TestMethod] - [ExpectedSeleniumException(typeof(JavaScriptException))] public void SelectMethod_InvalidXPathSelector_ExpectedException() { - this.RunInAllBrowsers(browser => + try { - browser.NavigateToUrl("/test/ClickTest"); - var elem = browser.Single("#span"); - elem.Single("///***-*///@@##@šš+š++++---><<>''", By.XPath); - }); + this.RunInAllBrowsers(browser => + { + browser.NavigateToUrl("/test/ClickTest"); + var elem = browser.Single("#span"); + elem.Single("///***-*///@@##@šš+š++++---><<>''", By.XPath); + }); + throw new System.Exception("SelectMethod_InvalidXPathSelector_ExpectedException was supposed to fail!!!"); + } + catch (System.Exception) + { + // ignore + // reason: Firefox driver and Chrome driver throw different exceptions + } } } } \ No newline at end of file From ea4227a06c1ed15bbfd30e3bf5f1ea72d9e87c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20=C5=A0est=C3=A1k?= Date: Sun, 2 Jun 2024 23:47:24 +0100 Subject: [PATCH 06/11] Removed unnecessary test reporter --- .github/workflows/main.yml | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 53a31bac..c1f34c77 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -57,11 +57,4 @@ jobs: browser: ${{ matrix.browser }} github-token: ${{ secrets.GITHUB_TOKEN }} build-configuration: "${{ matrix.environment == 'Production' && 'Release' || 'Debug' }}" - runtime-environment: "${{ matrix.environment }}" - - name: Test Reporter - uses: dorny/test-reporter@v1.9.1 - with: - artifact: ${{ matrix.browser }}-tests-Linux--Default # artifact name - name: ${{ matrix.browser }} UI Tests # Name of the check run which will be created - path: '*.trx' # Path to test results (inside artifact .zip) - reporter: dotnet-trx # Format of test results + runtime-environment: "${{ matrix.environment }}" \ No newline at end of file From 9a25ab818882659b8f1ed91ebcb6d7cf07dfbf55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20=C5=A0est=C3=A1k?= Date: Sun, 2 Jun 2024 23:57:53 +0100 Subject: [PATCH 07/11] Set default value "stable" to BrowserVersion --- .../Drivers/Implementation/ChromeHelpers.cs | 2 +- .../Drivers/Implementation/DictionaryExtensions.cs | 6 +++--- .../Drivers/Implementation/EdgeHelpers.cs | 2 +- .../Drivers/Implementation/FirefoxHelpers.cs | 2 +- .../Drivers/Implementation/InternetExplorerHelpers.cs | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/ChromeHelpers.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/ChromeHelpers.cs index 684becef..b714d724 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/ChromeHelpers.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/ChromeHelpers.cs @@ -16,7 +16,7 @@ public static ChromeDriver CreateChromeDriver(LocalWebBrowserFactory factory) options.AddArgument("disable-popup-blocking"); options.AddArguments(factory.Capabilities); - options.BrowserVersion = factory.Options.TryGet(nameof(options.BrowserVersion)); + options.BrowserVersion = factory.Options.TryGetOrDefault(nameof(options.BrowserVersion), "stable"); if (factory.GetBooleanOption("disableExtensions")) { diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/DictionaryExtensions.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/DictionaryExtensions.cs index 0e315754..aa301249 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/DictionaryExtensions.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/DictionaryExtensions.cs @@ -5,13 +5,13 @@ namespace Riganti.Selenium.Core.Drivers.Implementation public static class DictionaryExtensions { - public static T2 TryGet(this IDictionary dic, T1 key) + public static T2 TryGetOrDefault(this IDictionary dic, T1 key, T2 defaultValue) { - if(dic is not null && dic.TryGetValue(key, out T2 value) && value is T2) + if (dic is not null && dic.TryGetValue(key, out T2 value) && value is T2) { return value; } - return default; + return defaultValue; } } } \ No newline at end of file diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/EdgeHelpers.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/EdgeHelpers.cs index e53a1fc1..933dcdc2 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/EdgeHelpers.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/EdgeHelpers.cs @@ -11,7 +11,7 @@ public static EdgeDriver CreateEdgeDriver(LocalWebBrowserFactory factory) { }; - options.BrowserVersion = factory.Options.TryGet(nameof(options.BrowserVersion)); + options.BrowserVersion = factory.Options.TryGetOrDefault(nameof(options.BrowserVersion), "stable"); return new EdgeDriver(options); } } diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs index ffc7d0fa..7b70f238 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs @@ -31,7 +31,7 @@ public static FirefoxProfile GetFirefoxProfile() public static FirefoxOptions GetFirefoxOptions(System.Collections.Generic.IDictionary _options) { var options = new FirefoxOptions { Profile = GetFirefoxProfile() }; - options.BrowserVersion = _options.TryGet(nameof(options.BrowserVersion)); + options.BrowserVersion = _options.TryGetOrDefault(nameof(options.BrowserVersion), "stable"); return options; } } diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/InternetExplorerHelpers.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/InternetExplorerHelpers.cs index d9a0fc21..274b7d13 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/InternetExplorerHelpers.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/InternetExplorerHelpers.cs @@ -12,7 +12,7 @@ public static InternetExplorerDriver CreateInternetExplorerDriver(LocalWebBrowse { BrowserCommandLineArguments = "-private" }; - options.BrowserVersion = factory.Options.TryGet(nameof(options.BrowserVersion)); + options.BrowserVersion = factory.Options.TryGetOrDefault(nameof(options.BrowserVersion), "stable"); return new InternetExplorerDriver(options); } } From 9984bb2dd49840d4d08a7f6df048209d8ec871ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20=C5=A0est=C3=A1k?= Date: Mon, 3 Jun 2024 17:46:23 +0100 Subject: [PATCH 08/11] SeleniumManager testing --- .../Drivers/Implementation/ChromeHelpers.cs | 2 +- .../Implementation/DictionaryExtensions.cs | 4 +-- .../Drivers/Implementation/EdgeHelpers.cs | 2 +- .../Drivers/Implementation/FirefoxHelpers.cs | 16 +++++++-- .../Implementation/InternetExplorerHelpers.cs | 2 +- .../XunitTestSuiteRunner.cs | 26 -------------- .../seleniumconfig.json | 6 ++-- .../Profiles/seleniumconfig.chrome.json | 5 ++- .../SeleniumManagerTests.cs | 36 +++++++++++++++++++ .../seleniumconfig.json | 9 +++-- .../SeleniumManagerTests.cs | 22 ++++++++++++ .../seleniumconfig.json | 4 ++- 12 files changed, 93 insertions(+), 41 deletions(-) create mode 100644 src/Tests/Riganti.Selenium.Core.Samples.Tests/SeleniumManagerTests.cs create mode 100644 src/Tests/Riganti.Selenium.Core.UnitTests/SeleniumManagerTests.cs diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/ChromeHelpers.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/ChromeHelpers.cs index b714d724..684becef 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/ChromeHelpers.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/ChromeHelpers.cs @@ -16,7 +16,7 @@ public static ChromeDriver CreateChromeDriver(LocalWebBrowserFactory factory) options.AddArgument("disable-popup-blocking"); options.AddArguments(factory.Capabilities); - options.BrowserVersion = factory.Options.TryGetOrDefault(nameof(options.BrowserVersion), "stable"); + options.BrowserVersion = factory.Options.TryGet(nameof(options.BrowserVersion)); if (factory.GetBooleanOption("disableExtensions")) { diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/DictionaryExtensions.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/DictionaryExtensions.cs index aa301249..fc81069a 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/DictionaryExtensions.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/DictionaryExtensions.cs @@ -5,13 +5,13 @@ namespace Riganti.Selenium.Core.Drivers.Implementation public static class DictionaryExtensions { - public static T2 TryGetOrDefault(this IDictionary dic, T1 key, T2 defaultValue) + public static T2 TryGet(this IDictionary dic, T1 key) { if (dic is not null && dic.TryGetValue(key, out T2 value) && value is T2) { return value; } - return defaultValue; + return default; } } } \ No newline at end of file diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/EdgeHelpers.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/EdgeHelpers.cs index 933dcdc2..e53a1fc1 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/EdgeHelpers.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/EdgeHelpers.cs @@ -11,7 +11,7 @@ public static EdgeDriver CreateEdgeDriver(LocalWebBrowserFactory factory) { }; - options.BrowserVersion = factory.Options.TryGetOrDefault(nameof(options.BrowserVersion), "stable"); + options.BrowserVersion = factory.Options.TryGet(nameof(options.BrowserVersion)); return new EdgeDriver(options); } } diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs index 7b70f238..288e9706 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs @@ -1,5 +1,7 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.IO; +using Newtonsoft.Json; using OpenQA.Selenium.Firefox; using Riganti.Selenium.Core.Factories; @@ -16,7 +18,11 @@ static FirefoxHelpers() public static FirefoxDriver CreateFirefoxDriver(LocalWebBrowserFactory factory) { - return new FirefoxDriver(service, GetFirefoxOptions(factory.Options)); + Console.WriteLine("Firefox applies capabilities: " + JsonConvert.SerializeObject(factory.Capabilities)); + var ffOptions = GetFirefoxOptions(factory.Options); + Console.WriteLine("Firefox applies options: " + JsonConvert.SerializeObject(ffOptions)); + var driver = new FirefoxDriver(service, ffOptions); + return driver; } public static FirefoxProfile GetFirefoxProfile() @@ -30,8 +36,12 @@ public static FirefoxProfile GetFirefoxProfile() public static FirefoxOptions GetFirefoxOptions(System.Collections.Generic.IDictionary _options) { - var options = new FirefoxOptions { Profile = GetFirefoxProfile() }; - options.BrowserVersion = _options.TryGetOrDefault(nameof(options.BrowserVersion), "stable"); + var options = new FirefoxOptions + { + // Profile = GetFirefoxProfile() + }; + options.BrowserVersion = _options.TryGet(nameof(options.BrowserVersion)); + Console.WriteLine($"Requesting {options.BrowserVersion} version of browser"); return options; } } diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/InternetExplorerHelpers.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/InternetExplorerHelpers.cs index 274b7d13..d9a0fc21 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/InternetExplorerHelpers.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/InternetExplorerHelpers.cs @@ -12,7 +12,7 @@ public static InternetExplorerDriver CreateInternetExplorerDriver(LocalWebBrowse { BrowserCommandLineArguments = "-private" }; - options.BrowserVersion = factory.Options.TryGetOrDefault(nameof(options.BrowserVersion), "stable"); + options.BrowserVersion = factory.Options.TryGet(nameof(options.BrowserVersion)); return new InternetExplorerDriver(options); } } diff --git a/src/Integrations/Riganti.Selenium.xUnit/XunitTestSuiteRunner.cs b/src/Integrations/Riganti.Selenium.xUnit/XunitTestSuiteRunner.cs index f997524a..6f6a3fb8 100644 --- a/src/Integrations/Riganti.Selenium.xUnit/XunitTestSuiteRunner.cs +++ b/src/Integrations/Riganti.Selenium.xUnit/XunitTestSuiteRunner.cs @@ -23,30 +23,4 @@ public override void RunInAllBrowsers(ISeleniumTest testClass, Action "This reporter is a bit hack to close all resources when test execution ends."; - - // public bool IsEnvironmentallyEnabled => true; - - // public string RunnerSwitch => "riganti-selenium"; - - // public IMessageSink CreateMessageHandler(IRunnerLogger logger) - // { - // return new SeleniumMessageSink(); - // } - //} - //public class SeleniumMessageSink : IMessageSink - //{ - // public bool OnMessage(IMessageSinkMessage message) - // { - // if (message is TestAssemblyExecutionFinished) - // { - // if (Debugger.IsAttached) - // Debugger.Break(); - // } - // return true; - // } - //} } \ No newline at end of file diff --git a/src/Tests/Riganti.Selenium.Core.Samples.Assert.Tests/seleniumconfig.json b/src/Tests/Riganti.Selenium.Core.Samples.Assert.Tests/seleniumconfig.json index 1c93c9af..272c0ed3 100644 --- a/src/Tests/Riganti.Selenium.Core.Samples.Assert.Tests/seleniumconfig.json +++ b/src/Tests/Riganti.Selenium.Core.Samples.Assert.Tests/seleniumconfig.json @@ -1,8 +1,10 @@ { "factories": { "chrome:fast": { - "capabilities": [ "--window-size=1920,1080" , "--headless", "--disable-gpu" ], - "BrowserVersion": "124" + "capabilities": [ "--window-size=1920,1080", "--headless", "--disable-gpu" ], + "Options": { + "BrowserVersion": "124" + } } //"firefox:dev": { //} diff --git a/src/Tests/Riganti.Selenium.Core.Samples.Tests/Profiles/seleniumconfig.chrome.json b/src/Tests/Riganti.Selenium.Core.Samples.Tests/Profiles/seleniumconfig.chrome.json index 3d5c2f1c..bd6d19b5 100644 --- a/src/Tests/Riganti.Selenium.Core.Samples.Tests/Profiles/seleniumconfig.chrome.json +++ b/src/Tests/Riganti.Selenium.Core.Samples.Tests/Profiles/seleniumconfig.chrome.json @@ -1,7 +1,10 @@ { "factories": { "chrome:fast": { - "capabilities": [ "--no-sandbox" ] + "capabilities": [ "--no-sandbox" ], + "Options": { + "BrowserVersion": "124" + } } }, "baseUrls": [ diff --git a/src/Tests/Riganti.Selenium.Core.Samples.Tests/SeleniumManagerTests.cs b/src/Tests/Riganti.Selenium.Core.Samples.Tests/SeleniumManagerTests.cs new file mode 100644 index 00000000..3c329f9d --- /dev/null +++ b/src/Tests/Riganti.Selenium.Core.Samples.Tests/SeleniumManagerTests.cs @@ -0,0 +1,36 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Newtonsoft.Json; +using OpenQA.Selenium; + +[TestClass] +public class SeleniumManagerTests +{ + [TestMethod] + public void GetBinariesVersion113() + { + var data1 = SeleniumManager.BinaryPaths("--browser firefox --driver geckodriver --browser-version 113"); + Console.WriteLine("Firefox binaries: " + JsonConvert.SerializeObject(data1)); + Assert.IsNotNull(data1["browser_path"]); + Assert.IsNotNull(data1["driver_path"]); + + var data2 = SeleniumManager.BinaryPaths("--browser chrome --driver chromedriver --browser-version 113"); + Console.WriteLine("Chrome binaries: " + JsonConvert.SerializeObject(data2)); + Assert.IsNotNull(data2["browser_path"]); + Assert.IsNotNull(data2["driver_path"]); + } + + [TestMethod] + public void GetBinariesVersionStable() + { + var data1 = SeleniumManager.BinaryPaths("--browser firefox --driver geckodriver --browser-version stable"); + Console.WriteLine("Firefox binaries: " + JsonConvert.SerializeObject(data1)); + Assert.IsNotNull(data1["browser_path"]); + Assert.IsNotNull(data1["driver_path"]); + + var data2 = SeleniumManager.BinaryPaths("--browser chrome --driver chromedriver --browser-version stable"); + Console.WriteLine("Chrome binaries: " + JsonConvert.SerializeObject(data2)); + Assert.IsNotNull(data2["browser_path"]); + Assert.IsNotNull(data2["driver_path"]); + } +} \ No newline at end of file diff --git a/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json b/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json index 57398adc..ed146148 100644 --- a/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json +++ b/src/Tests/Riganti.Selenium.Core.Samples.Tests/seleniumconfig.json @@ -2,11 +2,14 @@ "factories": { "chrome:fast": { "capabilities": [ "--window-size=1920,1080", "--headless", "--disable-gpu" ], - "BrowserVersion": "124" + "Options": { + "BrowserVersion": "124" + } }, //"firefox:fast": { - // "capabilities": [ "--window-size=1920,1080", "--headless", "--disable-gpu" ], - // "BrowserVersion": "stable" + // "Options": { + // "BrowserVersion": "104" + // } //} //"chrome:coordinator": { // "options": { diff --git a/src/Tests/Riganti.Selenium.Core.UnitTests/SeleniumManagerTests.cs b/src/Tests/Riganti.Selenium.Core.UnitTests/SeleniumManagerTests.cs new file mode 100644 index 00000000..decc18c6 --- /dev/null +++ b/src/Tests/Riganti.Selenium.Core.UnitTests/SeleniumManagerTests.cs @@ -0,0 +1,22 @@ +using System; +using System.Text.Json; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using OpenQA.Selenium; + +[TestClass] +public class SeleniumManagerTests +{ + [TestMethod] + public void GetBinaries() + { + var data1 = SeleniumManager.BinaryPaths("--browser firefox --driver geckodriver --browser-version 104"); + Console.WriteLine("Firefox binaries: " + JsonSerializer.Serialize(data1)); + Assert.IsNotNull(data1["browser_path"]); + Assert.IsNotNull(data1["driver_path"]); + + var data2 = SeleniumManager.BinaryPaths("--browser chrome --driver chromedriver --browser-version 104"); + Console.WriteLine("Chrome binaries: " + JsonSerializer.Serialize(data2)); + Assert.IsNotNull(data2["browser_path"]); + Assert.IsNotNull(data2["driver_path"]); + } +} \ No newline at end of file diff --git a/src/Tests/Riganti.Selenium.Sandbox/seleniumconfig.json b/src/Tests/Riganti.Selenium.Sandbox/seleniumconfig.json index f44678e6..04e0096a 100644 --- a/src/Tests/Riganti.Selenium.Sandbox/seleniumconfig.json +++ b/src/Tests/Riganti.Selenium.Sandbox/seleniumconfig.json @@ -1,7 +1,9 @@ { "factories": { "firefox:fast": { - "BrowserVersion": "stable" + "Options": { + "BrowserVersion": "stable" + } } }, "baseUrls": [ From 7c2f89a8aac771b3f2a332c292ef3906d41584d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20=C5=A0est=C3=A1k?= Date: Mon, 3 Jun 2024 23:53:00 +0100 Subject: [PATCH 09/11] Cleanup in FirefoxHelpers --- .../Drivers/Implementation/FirefoxHelpers.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs index 288e9706..fc1de1c5 100644 --- a/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs +++ b/src/Core/Riganti.Selenium.Core/Drivers/Implementation/FirefoxHelpers.cs @@ -18,9 +18,7 @@ static FirefoxHelpers() public static FirefoxDriver CreateFirefoxDriver(LocalWebBrowserFactory factory) { - Console.WriteLine("Firefox applies capabilities: " + JsonConvert.SerializeObject(factory.Capabilities)); var ffOptions = GetFirefoxOptions(factory.Options); - Console.WriteLine("Firefox applies options: " + JsonConvert.SerializeObject(ffOptions)); var driver = new FirefoxDriver(service, ffOptions); return driver; } @@ -38,10 +36,9 @@ public static FirefoxOptions GetFirefoxOptions(System.Collections.Generic.IDicti { var options = new FirefoxOptions { - // Profile = GetFirefoxProfile() + Profile = GetFirefoxProfile() }; options.BrowserVersion = _options.TryGet(nameof(options.BrowserVersion)); - Console.WriteLine($"Requesting {options.BrowserVersion} version of browser"); return options; } } From cb5da4945fdc0109f926263a4ba66e4211a84e00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20=C5=A0est=C3=A1k?= Date: Mon, 3 Jun 2024 23:55:22 +0100 Subject: [PATCH 10/11] Increased preview version --- src/Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 07fa113a..035c0d52 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -4,7 +4,7 @@ RIGANTI Basic utilities for performing UI tests based on selenium on CI servers. selenium;ui;tests;riganti;ci - 3.0.0-preview16-browser-manager + 3.0.0-preview17-browser-manager Icon.png git https://github.com/riganti/selenium-utils From af74b7947e23c4f54273e5353dcc8e05a848ef90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20=C5=A0est=C3=A1k?= Date: Sun, 23 Jun 2024 14:41:58 +0100 Subject: [PATCH 11/11] Test fix - SelectMethod_InvalidXPathSelector_ExpectedException --- .../BrowsersStabilityUiTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Tests/Riganti.Selenium.Core.Samples.Tests/BrowsersStabilityUiTests.cs b/src/Tests/Riganti.Selenium.Core.Samples.Tests/BrowsersStabilityUiTests.cs index eac552b2..d16d6013 100644 --- a/src/Tests/Riganti.Selenium.Core.Samples.Tests/BrowsersStabilityUiTests.cs +++ b/src/Tests/Riganti.Selenium.Core.Samples.Tests/BrowsersStabilityUiTests.cs @@ -80,13 +80,14 @@ public void SelectMethod_InvalidXPathSelector_ExpectedException() var elem = browser.Single("#span"); elem.Single("///***-*///@@##@šš+š++++---><<>''", By.XPath); }); - throw new System.Exception("SelectMethod_InvalidXPathSelector_ExpectedException was supposed to fail!!!"); } catch (System.Exception) { // ignore // reason: Firefox driver and Chrome driver throw different exceptions + return; } + throw new System.Exception("SelectMethod_InvalidXPathSelector_ExpectedException was supposed to fail!!!"); } } } \ No newline at end of file