I'm trying to get started using Selenium with Chrome, i've had no previous trouble dealing with Selenium+Firefox, but i cannot seem to launch a Chrome browser now - every time I try, a Firefox browser appears instead.
Here is my setup:
$web_driver = new ChromeDriver("C:\chromedriver\chromedriver.exe");
$session = $web_driver->session('chrome');
I realise the first line is likely not to be correct. But i cannot think of how else to initiate Chrome.
Note: I have already downloaded the chrome web driver.
Here are the sources i used:
http://edvanbeinum.com/using-selenium-2-phpunit-to-automate-browser-testing
https://code.google.com/p/selenium/wiki/ChromeDriver
Many thanks.
Try using
$session = $web_driver->session('googlechrome');
instead of
$session = $web_driver->session('chrome');
You might want to take a look here and here.
$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::htmlUnitWithJS();
{
// For Chrome
$options = new ChromeOptions();
$prefs = array('download.default_directory' => 'c:/temp');
$options->setExperimentalOption('prefs', $prefs);
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
}
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
Related
I'm fairly new to PHP and playing around with Goutte/Guzzle to grab some basic information from a website after filling out a few forms.
However I have problems finding issues (there might be a ton of them) since I could not find a way to display or console log any of the results or problems. The script finishes with Code 0 but does not return anything. A tip on how to print out what is currently stored in $client would already go a long way.
Here is the whole code I'm trying to run with a bunch of comments for clarification. I'm sorry for using such a large block, but any of this could have issues.
<?php
use Goutte\Client;
use GuzzleHttp\Client as GuzzleClient;
class grabPlate
{
// WKZ
public function checkPlate
{
$goutteClient = new Client();
$guzzleClient = new GuzzleClient(array(
'cookies' => true,
'timeout' => 60,
));
$goutteClient->setClient($guzzleClient);
$crawler = $goutteClient->request('GET', 'https://kfz-portal.berlin.de/kfzonline.public/start.html?oe=00.00.11.000000');
//Click the first "Start" in the top left
$link = $crawler
->filter('a:contains("Start")')
->eq(0)
->link()
;
$crawler = $client->click($link);
//Check checkbox, fill in name and press the button
$buttonCrawlerNode = $crawler->selectButton('Weiter');
$form = $buttonCrawlerNode->form();
$form['gwt-uid-1']->tick();
$form['select2-hidden-accessible']->select('Herr');
$form['gwt-uid-4'] = 'John';
$form['gwt-uid-5'] = 'Doe';
$client->submit($form);
//Fill some Data into the forms and search
$buttonCrawlerNode = $crawler->selectButton('Button-3616');
$form = $buttonCrawlerNode->form();
$form['.kfzonline-KennzeichenGrossEb'] = 'AB';
$form['.kfzonline-KennzeichenGrossEn'] = '123';
$client->submit($form);
//Extract collection
$info = $crawler->extract('.collection');
//return 1 if something is inside collection, 0 if it's empty
if($info == NULL) {
return 1;
} else {
return 0;
}
}
}
?>
As I said just running the script in PHPStorm returns the status 0. However when plugging it into an API and accessing it, I get a server timeout response.
You should either use a Debugging. Install xDebug to do so in PHP. This is also easy to integrate into Phpstorm.
Alternatively use var_dump() for printing out debug information about variables of any type to console.
You can display the requested page inside your php page, you have to add this snippet :
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://www.facebook.com/');
echo $res->getBody();
I used these two resources as launching pad for my creation of a WSDL endpoint server.
https://odan.github.io/2017/11/20/implementing-a-soap-api-with-php-7.html
https://www.youtube.com/watch?v=e_7jDqN2A-Y&t=799s
By combining these two I was able to come up with a hybrid system that works. My issue that I am trying resolve right now is getting a response back from the api.php/endpoint server.
In the odan git example, it worked to the letter. But once I made changes to the code that requires objects. I started getting errors.
PHP Notice: Trying to get property of non-object
Here is a portion of the server code.
class wenoError
{
public $response = "Sucess";
public static function authenticate($header_params)
{
if($header_params->username == 'WEX' && $header_params->password == 'WEX1') return true;
else throw new SOAPFault('Wrong user/pass combination', 601);
}
/**
* #param string $payload
* #return string $delivery
*/
public function receivePayload($payload)
{
$xml = base64_decode($payload);
$fileName = 'message-'.rand().'.xml';
$file = file_put_contents('messages/'.$fileName, $xml);
$xml2json = simplexml_load_string($xml);
$jsonOut = json_encode($xml2json);
$arrayJson = json_decode($jsonOut, TRUE);
//$seeArray = print_r($arrayJson, true);
//file_put_contents('messages/converted-'.$fileName.'.json', $arrayJson['Header']['MessageID']);
$response = "Success";
return $response;
}
}
$serverUrl = "https://localhost/WenoErrors/api.php";
$options = [
'uri' => $serverUrl,
];
$server = new Zend\Soap\Server('wsdl', $options);
if (isset($_GET['wsdl'])) {
$soapAutoDiscover = new \Zend\Soap\AutoDiscover(new \Zend\Soap\Wsdl\ComplexTypeStrategy\ArrayOfTypeSequence());
$soapAutoDiscover->setBindingStyle(array('style' => 'document'));
$soapAutoDiscover->setOperationBodyStyle(array('use' => 'literal'));
$soapAutoDiscover->setClass('wenoError');
$soapAutoDiscover->setUri($serverUrl);
header("Content-Type: text/xml");
echo $soapAutoDiscover->generate()->toXml();
} else {
$soap = new \Zend\Soap\Server($serverUrl . '?wsdl');
$soap->setObject(new \Zend\Soap\Server\DocumentLiteralWrapper(new wenoError()));
$soap->handle();
}
What I don't understand is the error message of $response being a non-object. According to the PHP manual https://www.php.net/manual/en/language.oop5.properties.php
The property is set correctly at the top of the class, the property is declared and a value us set.
What went wrong?
UPDATE:
Adding the client code.
$client = new Zend\Soap\Client('https://localhost/WenoErrors/api.php?wsdl');
$delivery = $client->call('receivePayload',[['payload' => $message]]);
Dumping client yields:
C:\eRxGateway\www\apa\WenoErrors\clientapi.php:55:
object(client)[3]
public 'delivery' => null
UPDATE:
What finally worked for me was this change.
First change:
$server = new Zend\Soap\Server('wsdl', $options);
$server = new Zend\Soap\Server(null, $options);
Your code seems to work fine for me. Though, I am getting a different result then yours as below:
$client = new Zend\Soap\Client('http://localhost/test/api.php?wsdl');
$message = ' -> Hello World';
$delivery = $client->call('receivePayload',[['payload' => $message]]);
var_dump($delivery);
object(stdClass)#4 (1) {
["receivePayloadResult"]=>
string(7) "Success"
}
Step 1
Please try to remove all the '/tmp/wsdl-****' files from your /tmp directory. You seem to be on windows, so instead of /tmp it might be something else like C:\Windows\Temp. You can easily find which directory by going into your php.ini file and looking for the below directive.
soap.wsdl_cache_dir="/tmp"
Step 2
Also, for developing and testing purposes always put the below ini directive at the start of your client php file, which in your case is clientapi.php file.
ini_set("soap.wsdl_cache_enabled", 0);
You shouldn't be required to put this directive at the start of the server(api.php) file, but you can if the above still does not work for you.
I am very new to PHP and just got everything up and running. I am trying to self teach and I don't understand why my click function is failing. It sometimes will not process at all and other times I get the following error: https://www.screencast.com/t/ogZPogsLXjJ
<?php
require_once('vendor/autoload.php');
$host = 'http://localhost:4444/wd/hub';
$driver = RemoteWebDriver::create($host, DesiredCapabilities::firefox());
$driver->get('https://github.com');
$driver->findElement(WebDriverBy::xpath('/html/body/div[1]/header/div/div[2]/nav/ul/li[2]/a'))->click();
?>
I got it working:
$element = $driver->findElement(WebDriverBy::xpath('/html/body/div[1]/header/div/div[2]/nav/ul/li[2]/a'));
if ($element->isDisplayed()) {
$element->click();
}
I've read this question and answer on Stackoverflow. And this page. I've also read this page. I've also found this issue which doesn't resolved my problem either.
I'm using Selenium 2.53.1, FF 49.0.2 on Windows 10. Here is my code for the Facebook Webdriver, it's basically their example just extended by the profile settings:
$profile = new FirefoxProfile();
$profile->setPreference('browser.startup.homepage_override.mston', 'ignore');
$profile->setPreference('startup.homepage_welcome_url.additional', 'about:blank');
$profile->setPreference('browser.startup.homepage', 'about:blank');
$profile->setPreference('xpinstall.signatures.required', false);
$capabilities = DesiredCapabilities::firefox();
$capabilities->setCapability(FirefoxDriver::PROFILE, $profile);
$driver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities, 4444);
$driver->get('http://docs.seleniumhq.org/');
But I'm still getting this:
Am I doing something wrong?
There's an e missing at the end of mston. I would also set the browser.usedOnWindows10 to true:
$profile = new FirefoxProfile();
$profile->setPreference('browser.startup.page', 0);
$profile->setPreference('browser.startup.homepage', 'about:blank');
$profile->setPreference('browser.startup.homepage_override.mstone', 'ignore');
$profile->setPreference('browser.usedOnWindows10', true);
$profile->setPreference('xpinstall.signatures.required', false);
Im trying to do some mobile detection and this module is giving me problems.
$config = array('storage' => array('adapter' => 'Zend_Http_UserAgent_Storage_Session'));
try
{
//$bootstrap = $this->getInvokeArg('bootstrap');
//$userAgent = $bootstrap->getResource('useragent');
//Bootstrap::pr($userAgent);
//echo $userAgent->getUserAgent();
$ua = new Zend_Http_UserAgent($config);
Bootstrap::pr($ua);
Bootstrap::pr($ua->getDevice());
}catch(Exception $e)
{
Bootstrap::pr($e);
}
This is within the index action of my index controller.
Bootstrap::pr is basically a print_r wrapped with <pre>'s
when i open the page, the print_r for $ua works, but then it just dies and the page is blank. Can anyone help me out here?
Zend_Http_UserAgent depends on WURFL library, you have to configure it as described in the manual.
Check out WURFL - it may be just what you are looking for.