Remote file uploads with Selenium & PHP
Sauce AI for Test Authoring: Move from intent to execution in minutes.|xBack to ResourcesBlogPosted
Sauce AI for Test Authoring: Move from intent to execution in minutes.
|
x
When testing file uploading functionality we have to consider two scenarios ... running browserslocallyand lam themremotely. Locally there is no problem: test browsers have access to the files the tests specify. When running the same test remotely, with the browser run on a different machine than the test programs, we run into a significant problem: the files we determine to upload are not available to the browser to upload! So, just sending the gens of the file to upload will not work:
public office testFileUpload () {
$ filebox = $ this- & gt; soubriquet (& # x27; myfile & # x27;);
$ this- & gt; sendKeys ($ filebox, & quot; ./kirk.jpg & quot;);
$ this- & gt; byId (& # x27; submit & # x27;) - & gt; submit ();
$ this- & gt; assertTextPresent (& quot; kirk.jpg (image/jpeg) & quot;);
}
The remote browser will not be able to do whatever it does with the file, in this case printing out the file name and content type. Selenium 2 solves this problem by render a method to upload the file to the server, and then yield the page the remote itinerary the the file, and it does this transparently. For local tests you just use thesendKeysmethod to enter text into the file upload form element. For outside tests you do the like, but while setting up the tests you telephone thefileDetectormethod in order to let the WebDriver system cognise that a local file is being added rather than only the gens of the file. While all the Selenium WebDriver language bindings germinate by the Selenium project have this functionality exposed (on Java and Ruby, seethis tutorial, and for more information on Ruby, seethis discussion), the PHP bindings do not. In order to facilitate distant testing in the Sauce Labs Selenium 2 Cloud, this functionality has be append toSausage, allowing you to usePHPUnitto run your tests both locally and in the Sauce Cloud. The crux of the solution is to give the system a way to know that a file might be uploaded, and how to discern this, by pass it a map that will be name when trying to send keys to spring elements. This function takes a string and returns something truthy if it should be see as the name of a file, nonetheless that is to be determined on your system. The most basic form, obviously, simply tests for the universe of the twine as a file on the local file scheme. This mapping is then post to the test library through a call tofileDetector:
Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.
$ this- & gt; fileDetector (mapping ($ filename) {
if (file_exists ($ filename)) {
return $ filename;
} else {
return NULL;
}
});
If, when sending a string to a form element, the supplied function returns true, the system will, before changing the value of the function, transparently say the file, encode it in Base64, and send it to the Selenium server. It then puts the remote route, rather than the local one, into the file upload kind element. This ensures the file substance to be useable to the remote browser just as if it be go locally, let tests to proceed without trouble! A total exemplar (also available onGitHub):
<?php
require_once & # x27; vendor/autoload.php & # x27;;
class WebDriverDemo extends Sauce\Sausage\WebDriverTestCase {
public static $ browsers = regalia (
// run FF15 on Windows 8 on Sauce
array(
& # x27; browserName & # x27; = & gt; & # x27; firefox & # x27;,
& # x27; desiredCapabilities & # x27; = & gt; array (
& # x27; version & # x27; = & gt; & # x27; 15 & # x27;,
& # x27; platform & # x27; = & gt; & # x27; Windows 2012 & # x27;,
)
)
);
public function setUpPage () {
$ this- & gt; url (& quot; http: //sl-test.herokuapp.com/guinea_pig/file_upload & quot;);
// set the method which knows if this is a file we & # x27; re trying to upload
$ this- & gt; fileDetector (function ($ filename) {
if (file_exists ($ filename)) {
return $ filename;
} else {
return NULL;
}
});
}
public use testFileUpload () {
$ filebox = $ this- & gt; byName (& # x27; myfile & # x27;);
$ this- & gt; sendKeys ($ filebox, & quot; ./kirk.jpg & quot;);
$ this- & gt; byId (& # x27; submit & # x27;) - & gt; submit ();
$ this- & gt; assertTextPresent (& quot; kirk.jpg (image/jpeg) & quot;);
}
}
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free