java - 如何通过使用 Selenium Grid 将文件从本地计算机传输到远程 Web 服务器来上传文件

标签 java selenium selenium-webdriver selenium-grid remotewebdriver

使用 Selenium 网格上传文件:
代码

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Main 
{
    
    public static void main(String[] args) throws MalformedURLException
    {
          DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
          capabilities.setBrowserName("internet explorer");
    
          RemoteWebDriver driver = new RemoteWebDriver(new URL("http://URL:4444/wd/hub"), capabilities);
          driver.setFileDetector(new LocalFileDetector());
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    
          driver.get("https://url.de/index.xhtml");
          driver.findElement(By.xpath("//*[@id='form:sdsupload']/span")).click();
        
          WebElement addFile = driver.findElement(By.xpath("//input[@type='file']"));
          ((RemoteWebElement) addFile ).setFileDetector(new LocalFileDetector());
          addFile.sendKeys("C:\\daten\\test\\test2.xml");
    }
}
异常(exception)
Jun 26, 2020 3:47:43 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMATION: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.InvalidArgumentException: Attempting to upload file 'C:\daten\test\test2.xml' which does not exist.
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'xxx', ip: 'xxx', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: true, ie.browserCommandLineSwitches: , ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: false, ignoreZoomSetting: false, initialBrowserUrl: http://localhost:5494/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, webdriver.remote.sessionid: ae053a22-c088-402e-8de1-f25...}
Session ID: ae053a22-c088-402e-8de1-f25c4398ccbf
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:106)
    at de.xxx.xxx.keywords.Main.main(Main.java:41)
该文件存在于本地节点上,但不存在于远程节点上。如果文件位于远程节点上,则效果很好。该文件存在于本地节点上,但不在远程节点上:/image/f31Wb.png
我读到这个:
  • https://www.selenium.dev/documentation/en/remote_webdriver/remote_webdriver_client/
  • https://www.lambdatest.com/blog/how-to-download-upload-files-using-selenium-with-java/

  • 如何将文件传递到远程节点?
    编辑
    与 Firefox、IE 和 Edge Chromium 相同的错误
    火狐:
    Exception in thread "main" org.openqa.selenium.InvalidArgumentException: File not found: C:\daten\test\test3.xml
    Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
    System info: host: 'xxx', ip: 'xxx', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_171'
    Driver info: org.openqa.selenium.remote.RemoteWebDriver
    Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 60.5.0, javascriptEnabled: true, moz:accessibilityChecks: false, moz:geckodriverVersion: 0.25.0, moz:headless: false, moz:processID: 624, moz:profile: C:\Users\username\Ap..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 10.0, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, webdriver.remote.sessionid: 8801c61e-6d61-488b-8f86-c0a...}
    Session ID: 8801c61e-6d61-488b-8f86-c0a1fb2f2df8
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
        at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
        at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
        at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
        at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:106)
        at de.xxx.xxx.keywords.Main.main(Main.java:52)
    
    当我将文件复制到远程客户端时,它再次运行良好:(
    调试:
    Debug with Firefox

    最佳答案

    这个错误信息...

    Exception in thread "main" org.openqa.selenium.InvalidArgumentException: Attempting to upload file 'C:\daten\test2.xml' which does not exist.
    
    ...暗示客户端计算机上不存在所需的文件。

    本地文件检测器
    Local File Detector允许将文件从客户端机器传输到远程服务器。如果测试需要将文件上传到 Web 应用程序,远程 WebDriver可以在运行时自动将文件从本地机器传输到远程 Web 服务器。这允许从运行测试的远程机器上传文件。默认情况下不启用,可以按如下方式启用:
  • java :
    driver.setFileDetector(new LocalFileDetector());
    
  • Python:
    from selenium.webdriver.remote.file_detector import LocalFileDetector
    
    driver.file_detector = LocalFileDetector()
    
  • C#:
    var allowsDetection = this.driver as IAllowsFileDetection;
    if (allowsDetection != null)
    {
       allowsDetection.FileDetector = new LocalFileDetector();
    }
    
  • ruby :
    @driver.file_detector = lambda do |args|
      # args => ["/path/to/file"]
      str = args.first.to_s
      str if File.exist?(str)
    end
    
  • JavaScript:
    var remote = require('selenium-webdriver/remote');
    driver.setFileDetector(new remote.FileDetector); 
    
  • Kotlin :
    driver.fileDetector = LocalFileDetector()
    

  • 这个用例
    如果您在 Selenium Grid 上运行测试那么你需要让你的远程驱动程序知道需要上传的文件驻留在本地机器上而不是远程机器上。在这些情况下,要将文件从客户端计算机上传到远程服务器,WebDriver 可以在运行时自动将文件从本地计算机传输到远程 Web 服务器,您可以使用以下代码块:
    WebElement addFile = driver.findElement(By.xpath("//input[@type='file']"));
    ((RemoteWebElement)addFile).setFileDetector(new LocalFileDetector());
    addFile.sendKeys("C:\\daten\\test2.xml");
    

    奥特罗
    Selecting and uploading files while running your tests on Selenium Grid

    关于java - 如何通过使用 Selenium Grid 将文件从本地计算机传输到远程 Web 服务器来上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62595459/

    相关文章:

    java - 替代 Java 中的 goto 语句

    java - 尝试从数组中查找重复数字时出现 "java.lang.ArrayIndexOutOfBoundsException"异常

    java - 有没有办法在 testng.xml 内的测试中将不同的参数值发送到不同的类

    python - 使用 xvfb + PyCharm + vagrant 设置测试

    java - 如何在谷歌浏览器中获取 Activity 标签页索引?

    java - 无法使用 C++ TAO 客户端连接到 java 1.3 NamingService

    java - 突破游戏多键处理

    java - 代码未将密码值粘贴到网页的密码字段中

    java - 从 3 个下拉列表中选择项目,并依赖于第一个下拉列表

    java - 无法在 Maven 中使用 Selenium 连接到二进制 FirefoxBinary