Selenium RC WaitForPageToLoad Hangs - php

I am trying to get Selenium RC up and running for doing some automated testing on my website. I am finding that I constantly want to verify that I haven't broken any features, and manual testing is starting to become tiresome.
However, I can't seem to get Selenium RC to work with WaitForPageToLoad.
I tried copying the basic example that they give in the selenium documentation, but the test always gets stuck at: $this->waitForPageToLoad("30000"); I can see that it gets that far in the window that it brings up and that the page appears to have loaded correctly (we are at a google search result page). But the test fails with a timeout.
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
/**
* Description of Test
*
* #author brian
*/
class Test extends PHPUnit_Extensions_SeleniumTestCase {
function setUp() {
$this->setBrowser("*safari");
$this->setBrowserUrl("http://www.google.com/");
}
function testMyTestCase() {
$this->open("/");
$this->type("q", "selenium rc");
$this->click("btnG");
$this->waitForPageToLoad("30000");
$this->assertTrue($this->isTextPresent("Results * for selenium rc"));
}
}
What is even more interesting is that if I refresh the page when it is waiting, everything continues on as expected. So it would appear as though the waitForPageToLoad isn't realizing that the page has already loaded.

The example in the Selenium RC documentation is obsolete. Google changed the way their home page worked quite a while ago, and it is no longer a simple HTML page. Pressing the search button is now an AJAX-type operation that sends the search request and gets back a JSON response that is processed by the JavaScript code in the page. So the page never is re-loaded, and WaitForPageToLoad() eventually times out.

There is also another possible cause of this situation that I ran into just now. According to the documentation, if you call ANY SELENIUM COMMANDS in between loading a page and calling waitForPageToLoad, then it is possible that waitForPageToLoad will hang. (If I understand it correctly, it is technically a race condition between the test script and selenium server, so it happens sometimes, not necessarily all the time).
In most cases, the page load is caused by a click event. When you have a test script like:
$this->click("/some/path");
// <<- NO SELENIUM COMMANDS HERE
$this->waitForPageToLoad("30000");
Make extra sure that no selenium commands ever accidentally get inserted into the marked area.
While this is not technically the same problem that the OP posted about, it has the same error message, and I couldn't find this information without digging around quite a bit. Hopefully this is easier to find for other people in the future.

I have observed same problem many times. Hence I did not use this command when user is not navigating away from current page. It hangs at times and using IsElementPresent in while loop and exit after it return true.

An alernative to "WaitForPageToLoad()" Is to wait for an element to be present.
$SECONDS = 360;
for ($second = 0; ; $second++) {
if ($second >= $SECONDS) $this->fail("TIMEOUT");
try {
if ($this->isElementPresent("edit-submit")) break;
} catch (Exception $e) {}
sleep(1);
}
This code will loop for 360 seconds, checking if the value (edit-submit) is present each second. ("sleep(1)"). It essentially will achieve the same result as WaitForPageToLoad, but you can specify an absolute target.

Related

Best way to guarantee a job is been executed

I have a script that is running continuously in the server, in this case a PHP script, like:
php path/to/my/index.php.
It's been executed, and when it's done, it's executed again, and again, forever.
I'm looking for the best way to be notified if that event stop running(been executed).
There are many reasons why it stops been called, like server memory, new deployment, human error... etc.
I just want to be notified(email, sms, slack...) if that script was not executed for certain amount of time(like 1 hour, 1 day, etc...)
My server is Ubuntu living in AWS.
An idea:
I was thinking on having an index in REDIS/MEMCACHED/ETC with a TTL. Every time the script run, renovate that TTL for this index.
If the script stop working for that TTL time, this index will expire. I just need a way to trigger a notification when that expiration happen, but looks like REDIS/MEMCACHED are not prepared for that
register_shutdown_function might help, but might not... https://www.php.net/manual/en/function.register-shutdown-function.php
I can't say i've ever seen a script that needs to run indefinitely in PHP. Perhaps there is another way to solve the problem you are after?
Update - Following your redis idea, I'd look at keyspace notifications. https://redis.io/topics/notifications
I've not tested the idea since I'm not actually a redis user. But it may be possible to subscribe to capture the expiration event (perhaps from another server?) and generate your notification.
There's no 'best' way to do this. Ultimately, what works best will boil down to the specific workflow you're supporting.
tl;dr version: Find what constitutes success and record the most recent time it happened. Use that for your notification trigger in another script.
Long version:
That said, persistent storage with a separate watcher is probably the most straight-forward way to do this. Record the last successful run, and then check it with a cron job every so often.
For what it's worth, for scripts like this I generally monitor exit codes or logs produced by the script in question. This isolates the error notification process from the script itself so a flaw in the script (hopefully) doesn't hamper the notification.
For a barebones example, say we have a script to invoke the actual script... (This is very much untested pseudo-code)
<?php
//Run and record.
exec("php path/to/my/index.php", $output, $return_code);
//$return_code will be 255 on fatal errors. You can use other return codes
//with exit in your called script to report other fail states.
if($return_code == 0) {
file_put_contents('/path/to/folder/last_success.txt', time());
} else {
file_put_contents('/path/to/folder/error_report.json', json_encode([
'return_code' => $return_code,
'time' => time(),
'output' => implode("\n", $output),
//assuming here that error output isn't silently logged somewhere already.
], JSON_PRETTY_PRINT));
}
And then a watcher.php that monitors these files on a cron job.
<?php
//Notify us immediately on failure maybe?
//If you have a lot of transient failures it may make more sense to
//aggregate and them in a single report at a specific time instead.
if(is_file('/path/to/folder/error_report.json')) {
//Mail details stored in JSON here.
//rename file so it's recorded, but we don't receive it again.
rename('/path/to/folder/error_report.json', '/path/to/folder/error_report.json'.'-sent-'.date('Y-m-d-H-i-s'));
} else {
if(is_file('/path/to/folder/last_success.txt')) {
$last_success = intval(file_get_contents('/path/to/folder/last_success.txt'));
if(strtotime('-24 hours') > $last_success) {
//Our script hasn't run in 24 hours, let someone know.
}
} else {
//No successful run recorded. Might want to put code here if that's unexpected.
}
}
Notes: There are some caveats to the specific approach displayed above. A script can fail in a non-fatal way and if you're not checking for it this example could record that as a successful run. For example, permissions errors causing warnings but the script still runs it's full course and exits normally without hitting an exit call with a specific return code. Our example invoker here would log that as a successful run - even though it isn't.
Another option is to log success from your script and only check for error exits from the invoker.

Operation timeout after switching back from popup window to main window

I'm using PHPUnit with the Selenium2 extension.
I'm opening a popup window, entering data and hit the submit button - after that the popup window closes.
Later then I'm switching back to the main window - all that works flawlessly. But after switching back no other steps of my tests are executed and the test fails with the following error message:
PHPUnit_Extensions_Selenium2TestCase_NoSeleniumException: Error connection[28] to http://localhost:4444/wd/hub/session/d6977d2b-76ac-4754-9a08-5119413b0965/element/4/submit: Operation timed out after 60004 milliseconds with 0 bytes received
For the sake of completeness the code:
$windowHandles = $this->windowHandles();
$this->window($windowHandles[1]);
$this->byCssSelector('input[id=email]')->value($fbUsername);
$this->byCssSelector('input[id=pass]')->value($fbPassword);
$this->byCssSelector('input[id=u_0_1]')->submit();
$this->window($windowHandles[0]);
Did I miss some required step? Do I have to wait for something? Any pointers would help.
#akluth, a bug was discovery and will be fix. You can see more information here. I'm waiting for a new release.
I am not a PHP guy but I hope the following code will give you some ideas how to handle this. Note: the following is C# code
//You probably missing this the concept of handling current and original handle
string currentHandle = driver.CurrentWindowHandle;
ReadOnlyCollection<string> originalHandles = driver.WindowHandles;
// Cause the pop-up to appear
driver.FindElement(By.XPath("//*[#id='webtraffic_popup_start_button']")).Click();
// WebDriverWait.Until<T> waits until the delegate returns
// a non-null value for object types. We can leverage this
// behaviour to return the pop-up window handle.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
string popupWindowHandle = wait.Until<string>((d) =>
{
string foundHandle = null;
// Subtract out the list of known handles. In the case of a single
// pop-up, the newHandles list will only have one value.
List<string> newHandles = driver.CurrentWindowHandles.Except(originalHandles).ToList();
if (newHandles.Count > 0)
{
foundHandle = newHandles[0];
}
return foundHandle;
});
driver.SwitchTo().Window(popupWindowHandle);
// Do whatever you need to on the pop-up browser, then...
driver.Close();
driver.SwitchToWindow(currentHandle);
As, you said all that works flawlessly. But after switching back no other steps of my tests are executed and the test fails
because the you have not correctly switch back to the original window handle.
This code is copy pasted from here
Thanks #JimEvans for explaning this process.
Looks like your browser is upgraded. You need to update your selenium libraries accordingly.

Yii Php Executing asynchronous background request

Hi i'm trying to execute a LONG RUNNING request (action) in background.
function actionRequest($id){
//execute very long process here in background but continue redirect
Yii::app()->user->setFlash('success', "Currently processing your request you may check it from time to time.");
$this->redirect(array('index', 'id'=>$id));
}
What i'm trying to achieve is to NOT have the user waiting for the request to be processed since it generally takes 5-10min, and the request usually goes to a timeout, and even if I set the timeout longer, waiting for 5-10 min. isn't a good user experience.
So I want to return to the page immediately notifying the user that his/her request is being processed, while he can still browse, and do other stuff in the application, he/she can then go back to the page and see that his/her request was processed.
I've looked into Yii extensions backjob, It works, the redirect is executed immediately (somehow a background request), but when doing other things, like navigating in the site, it doesn't load, and it seems that the request is still there, and i cannot continue using the application until the request is finished.
A similar extension runactions promises the same thing, but I could not even get it to work, it says it 'touches a url', like a fire and forget job but doesn't work.
I've also tried to look into message queuing services like Gearman, RabbitMQ, but is really highly technical, I couldn't even install Gearman in my windows machine so "farming" services won't work for me. Some answers to background processing includes CRON and AJAX but that doesn't sound too good, plus a lot of issues.
Is there any other workaround to having asynchronous background processing? I've really sought hard for this, and i'm really not looking for advanced/sophisticated solutions like "farming out work to several machines" and the likes. Thank You very much!
If you want to be able to run asynchronous jobs via Yii, you may not have a choice but to dabble with some AJAX in order to retrieve the status of the job asynchronously. Here are high-level guidelines that worked for me. Hopefully this will assist you in some way!
Setting up a console action
To run background jobs, you will need to use Yii's console component. Under /protected/commands, create a copy of your web controller that has your actionRequest() (e.g. /protected/commands/BulkCommand.php).
This should allow you to go in your /protected folder and run yiic bulk request.
Keep in mind that if you have not created a console application before, you will need to set up its configuration similar to how you've done it for the web application. A straight copy of /protected/config/main.php into /protected/config/console.php should do 90% of the job.
Customizing an extension for running asynchronous console jobs
What has worked for me is using a combination of two extensions: CConsole and TConsoleRunner. TConsoleRunner uses popen to run shell scripts, which worked for me on Windows and Ubuntu. I simply merged its run() code into CConsole as follows:
public function popen($shell, $redirectOutput = '')
{
$shell = $this->resolveCommandLine($shell, false, $redirectOutput);
$ret = self::RETURN_CODE_SUCCESS;
if (!$this->displayCommands) {
ob_start();
}
if ($this->isWindows()) {
pclose(popen('start /b '.$shell, 'r'));
}
else {
pclose(popen($shell.' > /dev/null &', 'r'));
}
if (!$this->displayCommands) {
ob_end_clean();
}
return $ret;
}
protected function isWindows()
{
if(PHP_OS == 'WINNT' || PHP_OS == 'WIN32')
return true;
else
return false;
}
Afterwards, I changed CConsole's runCommand() to the following:
public function runCommand($command, $args, $async = false, &$outputLines = null, $executor = 'popen')
{
...
switch ($executor) {
...
case 'popen':
return $this->popen($shell);
...
}
}
Running the asynchronous job
With the above set up, you can now use the following snippet of code to call yiic bulk request we created earlier.
$console = new CConsole();
$console->runCommand('bulk request', array(
'--arg1="argument"',
'--arg2="argument"',
'--arg3="argument"',
));
You would insert this in your original actionRequest().
Checking up on the status
Unfortunately, I'm not sure what kind of work your bulk request is doing. For myself, I was gathering a whole bunch of files and putting them in a folder. I knew going in how many files I expected, so I could easily create a controller action that verifies how many files have been created so far and give a % of the status as a simple division.

Is it possible to update screen and wait for user input at the same time with PHP?

I want to write a small management tool to oversee my server processes, my problem is now, how could i wait for user input and at same time update the screen with current stats? Is it even possible with PHP-CLI or are there any tricks for doing this I missing currently?
I have looked into newt and ncurses PECL extensions but both doesn't seem to fit my needs.
Go for libevent http://www.php.net/manual/en/book.libevent.php
You can run your main loop while listening to console with a code roughly like this one:
<?php
// you need libevent, installable via PEAR
$forever=true;
$base=event_base_new();
$console=event_buffer_new(STDIN,"process_console");
event_buffer_base_set($console,$base);
event_buffer_enable($console,EV_READ);
while ($forever) {
event_base_loop($base,EVLOOP_NONBLOCK); // Non blocking poll to console listener
//Do your video update process
}
event_base_free($base); //Cleanup
function process_console($buffer,$id) {
global $base;
global $forever;
$message='';
while ($read = event_buffer_read($buffer, 256)) {
$message.=$read;
}
$message=trim($message);
print("[$message]\n");
if ($message=="quit") {
event_base_loopexit($base);
$forever=false;
}
else {
//whatever.....
}
}
I don't think you can do it with PHP CLI. As I know, when interpret the script with PHP, you can only view the final output.
I think you do want ncurses. If you can convert the simple example C code here, which you should be able to with the PHP wrapper, you'd have your "bootstrap" for solving your problem.
Make sure to blog your code somewhere! :)
My advice would be to try to avoid any solutions that talk about leaving processes running whilst exiting PHP. Here is a really simple example of how to do it with a bit of jQuery:
window.setInterval(checkstat, 10000); //10 second interval
function checkstat() {
//Change a div with id stat to show updating (don't need this but it's nice)
$('#stat').html('Updating...');
$.get('/getmystats.php?option=blah', function(data) {
//Update the results when the data is returned.
$('#stat').html(data);
});
}
If you are need to update more than one area on your page, you can do one call but return JSON or XML and then populate the bits as required.

Cleaning up cookies whenever Internet Explorer test starts with selenium 2 php webdriver

I have a set of Selenium 2 tests (using chibimagic's php bindings) running perfectly (phpunit) on firefox and other browsers that actually create a new profile each time a test is run.
With Internet Explorer this doesn't happen, the browsers keeps all the cookies whenever a new test is run. I can of course undo whatever was done in the session in the tests (logout etc) but that will increase the running time of the tests (not desirable).
What I would like to happen is for Internet Explorer to clean the cookies every time a new test is run. I tried $this->delete_all_cookies() and $this->driver->delete_all_cookies before each test but that doesn't seem to do anything.
I know you can set desiredCapabilities which would supposedly start a clean session but that doesn't seem to work. I tried
$this->driver = WebDriver_Driver::InitAtHost("somehost.com" ,"4444", "internet explorer",array('ENSURING_CLEAN_SESSION' => true));
And if I was logged in in the previous set of tests, I would still be logged in in the new set of sets.
Some help would be appreciated greatly.
I was able to solve this problem for IE8+ by using the IE web driver server and telling it to start IE in "InPrivate" mode. When starting the server, use a command line similar to:
IEDrvierServer_win32.ee /force-createprocess /ie-switches=-private
I'm not sure what the syntax is in PHP, but you'll need to use a remote web driver.
You may also need to set this registry value:
[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main] - TabProcGrowth = (DWORD)0
There's a function called reset() within Selenium2Driver.php that will reset the session, thus removing all cookies. I just created a Given scenario step that I call before each test, which calls reset(). Here's the code:
/**
* #Given /^I am not logged in$/
*/
public function iAmNotLoggedIn() {
$this->getSession()->getDriver()->reset();
}
I know this is probably too late to help you, but hopefully it will help someone!

Categories