简体   繁体   English

如何使用 PHP 和 Facebook WebDriver 上传文件?

[英]How can I upload the file using PHP and Facebook WebDriver?

I am using PHP and Behat (no mink) with Selenium using Facebook WebDriver.我在使用 Facebook WebDriver 的 Selenium 中使用 PHP 和 Behat(无貂)。 I am working on my last test case which is to upload a local file (image) saved on my computer (using a Mac).我正在处理我的最后一个测试用例,即上传保存在我的计算机上的本地文件(图像)(使用 Mac)。 How can I upload the file using PHP and Facebook WebDriver?如何使用 PHP 和 Facebook WebDriver 上传文件?

I get the element of the "upload" button and then the pop up comes to choose the file.我得到了“上传”按钮的元素,然后弹出来选择文件。

$this>webDriver>setFileDetector(new\Facebook\WebDriver\Remote\LocalFileDetector());

// upload the file and submit the form
$this>webDriver>getKeyboard()>sendKeys("/Users/Guest/Documents/image.jpg/;

But this is not working.但这是行不通的。

I also get this error :我也收到此错误:

Fatal error: Uncaught Error: Call to undefined method Facebook\\WebDriver\\Remote\\RemoteWebDriver::setFileDetector() in /Users/Guest/Documents/features/bootstrap/FeatureContext.php:232致命错误:未捕获错误:调用 /Users/Guest/Documents/features/bootstrap/FeatureContext.php:232 中未定义的方法 Facebook\\WebDriver\\Remote\\RemoteWebDriver::setFileDetector()

Consider the following html element.考虑以下 html 元素。

<input type="file" id="file_input"></input>

Your upload code will look like this:您的上传代码如下所示:

<?php    
use Facebook\WebDriver\Remote\LocalFileDetector;

//getting the input element
$fileInput = $driver->findElement(WebDriverBy::id('file_input'));

//set the fileDetector
$fileInput->setFileDetector(new LocalFileDetector());

$filePath = 'D:\\work\\udhav.pdf';
$fileInput->sendKeys($filePath);

setFileDetector is a method on the RemoteWebElement class, not the RemoteWebDriver class. setFileDetector 是 RemoteWebElement 类上的一个方法,而不是 RemoteWebDriver 类。 You must find the element and then call setFileDetector() on it.您必须找到该元素,然后对其调用 setFileDetector()。 See this example:看这个例子:

Source: https://github.com/facebook/php-webdriver/wiki/Upload-a-file来源: https : //github.com/facebook/php-webdriver/wiki/Upload-a-file

  // getting the input element
  $fileInput = $driver->findElement(WebDriverBy::id('file_input'));
  // set the file detector
  $fileInput->setFileDetector(new LocalFileDetector());
  // upload the file and submit the form
  $fileInput->sendKeys($filePath)->submit();

A note: the line "$fileInput->sendKeys($filePath)->submit();"注意:“$fileInput->sendKeys($filePath)->submit();”行didn't work for me and was causing errors.对我不起作用并导致错误。 I removed the "submit" function call and got this to work: "$fileInput->sendKeys($filePath);".我删除了“提交”函数调用并使其工作:“$fileInput->sendKeys($filePath);”。 After this line, you'll want to find the form submit button and click it like you would any other form.在这一行之后,您需要找到表单提交按钮并像点击任何其他表单一样单击它。 This sendKeys call takes the place of clicking the browse button and selecting a file to upload.此 sendKeys 调用代替单击浏览按钮并选择要上传的文件。 If you take a screenshot after this function call, you can see the selected file's name next to the "Browse..." button just like you would in a manual test of the form.如果您在此函数调用后截取屏幕截图,您可以在“浏览...”按钮旁边看到所选文件的名称,就像您在表单的手动测试中一样。

Link to documentation about the method: https://facebook.github.io/php-webdriver/community/Facebook/WebDriver/Remote/RemoteWebElement.html#method_setFileDetector链接到有关该方法的文档: https : //facebook.github.io/php-webdriver/community/Facebook/WebDriver/Remote/RemoteWebElement.html#method_setFileDetector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM