Skip to content

Commit

Permalink
Fix 801 regression: custom ToString for options (net)
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Dec 9, 2024
1 parent c1d7fa5 commit a2101f7
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.26.0</version>
<version>4.27.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public WebDriver getSeleniumDriver(String browser, String remoteUrl,String drive
//Creates either local or remote web driver
objectToInstantiate="WebDriver";
log.debug("Setting up WebDriver, browser: "+browser+", url: "+remoteUrl);
lastOptionString=opt.toString();
lastOptionString=reflect.getOptionsObjAsString(opt);
log.trace("Option string: "+lastOptionString.replace("\n", "").replace("\r", ""));
if (remoteUrl==null || "".equals(remoteUrl.trim())) {
ensureLocalDriverDownloaded(browser, driverVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public Object getOptionsObj(String browser, String[] args) throws Exception { //
String className=getSeleniumClassName(browser, "Options");
return Class.forName(className).getConstructor().newInstance();
}
// #801 The toString gets all options in java, but needs a custom implementation in net
public String getOptionsObjAsString(Object opt) throws Exception { //NOSONAR
return opt.toString();
}
public void setCapability(Object opt, String key, Object value) throws Exception { //NOSONAR
opt.getClass().getMethod("setCapability", String.class, Object.class).invoke(opt, key, value);
}
Expand Down
8 changes: 6 additions & 2 deletions java/src/test/java/test4giis/selema/core/TestDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ public void testLocalWebDriverFirefox() {
if (!isLocal()) return;
SeleniumDriverFactory factory=new SeleniumDriverFactory();
WebDriver driver=factory.getSeleniumDriver("firefox", "", "", null, null, null);
assertOptions(factory, "{browserName:firefox,moz:firefoxOptions:{}}");
// #801 no toString method implemented for firefox
assertOptions(factory, Parameters.isJava()
? "{browserName:firefox,moz:firefoxOptions:{}}"
: "OpenQA.Selenium.Firefox.FirefoxOptions");
driver.close();
}

Expand Down Expand Up @@ -98,9 +101,10 @@ public void testHeadlessWebDriverWithOptions() {
caps.put("testprefix:key1", "value1");
caps.put("testprefix:key2", "value2");
WebDriver driver=factory.getSeleniumDriver("chrome", "", "", caps, chromeHeadlesArgument, null);
// #801 the toString method is not able to get other custom capabilities than the standard and chromeOptions
assertOptions(factory, Parameters.isJava() //different order on net
? "{browserName:chrome,goog:chromeOptions:{args:[--headless,--remote-allow-origins=*]},testprefix:key1:value1,testprefix:key2:value2}"
: "{browserName:chrome,testprefix:key1:value1,testprefix:key2:value2,goog:chromeOptions:{args:[--headless,--remote-allow-origins=*]}}");
: "{browserName:chrome,goog:chromeOptions:{args:[--headless,--remote-allow-origins=*]}}");
driver.close();
}

Expand Down
31 changes: 26 additions & 5 deletions net/Selema/Giis.Selema.Portable.Selenium/SeleniumObjects.N.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.IO;
using System.Reflection;
using OpenQA.Selenium.Chromium;

namespace Giis.Selema.Portable.Selenium
{
Expand All @@ -18,13 +19,33 @@ public class SeleniumObjects
{
internal readonly Logger log = LogManager.GetCurrentClassLogger();

public object GetOptionsObj(string browser, string[] arguments)
public object GetOptionsObj(string browser, string[] arguments)
{
string clasName = GetSeleniumClassName(browser, "Options");
return Activator.CreateInstance(Type.GetType(clasName));
}

// #801 The toString gets all options in java, but needs a custom implementation in net
// because it was removed in version 2.7.0
// Custom implementation only for chrome and edge to generate the same string than java generates,
// including part of the options (other additional capabilities can't be obtained because they are private)
public string GetOptionsObjAsString(object obj)
{
string clasName = GetSeleniumClassName(browser, "Options");
return Activator.CreateInstance(Type.GetType(clasName));
}
if (obj is ChromiumOptions)
return GetChromiumOptionsObjAsString((ChromiumOptions)obj);
return obj.ToString();
}
public string GetChromiumOptionsObjAsString(ChromiumOptions optObj)
{
string arguments = optObj.Arguments.Count == 0 ? "" : "args:[" + string.Join(",", optObj.Arguments) + "]";
string optString = "{"
+ "browserName:" + optObj.BrowserName
+ "," + optObj.CapabilityName + ":{" + arguments + "}"
+ "}";
return optString;
}

public void SetCapability(object opt, string key, object value)
public void SetCapability(object opt, string key, object value)
{
MethodInfo setCapability = opt.GetType().GetMethod("AddAdditionalOption", new Type[] { typeof(string), typeof(object) });
setCapability.Invoke(opt, new object[] { key, value });
Expand Down
4 changes: 2 additions & 2 deletions net/Selema/Selema.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>netstandard2.0</TargetFramework>
Expand Down Expand Up @@ -62,7 +62,7 @@

<PackageReference Include="WebDriverManager" Version="2.17.4" />

<PackageReference Include="Selenium.WebDriver" Version="4.26.1" />
<PackageReference Include="Selenium.WebDriver" Version="4.27.0" />
</ItemGroup>

</Project>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions net/SelemaTest/Translated/Test4giis.Selema.Core/TestDriver.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions net/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
import java.util.Map;
public class SeleniumObjects {
public Object getOptionsObj(String browser, String[] arguments) { return null; }
public String getOptionsObjAsString(Object obj) { return null; }
public void setCapability(Object opt, String key, Object value) { }
public void addArguments(Object opt, String[] args) { }
public Object getDriverObj(String browser, Object opt) { return null; }
Expand Down

0 comments on commit a2101f7

Please sign in to comment.