Asked  9 Months ago    Answers:  5   Viewed   57 times

During test I need to do following:

  • Click button which leads to ajax request and redirect after that
  • Check that user has been redirected to correct page

My Code:

$this->byId('reg_email')->value('test@example.com');
$this->byId('reg_password')->value('seecret');
// No form here, so i can't just call submit()
// This click invokes ajax request
$this->byId('reg_submit')->click();

// Check page content (this page should appear after redirect)
$msg = $this->byCssSelector('h1')->text();
$this->assertEquals('Welcome!', $msg);

Problem

  • Message check goes right after click, not before ajax request and page redirect

Solution, I do not like:

  • Add sleep(3); before content check.

I do not like it because:

  • It is silly
  • In case of fast responses I am going to lose time, in case of long requests I am going to get content check before ajax request finishes.

I wonder, is there any way to track ajax request+refresh and check for content just in time?

My setup:

  • PHP 5.4, 5.5 also available
  • PHPUnit 3.8
  • Selenium RC integration for PHPUnit 1.3.1
  • Selenium-server-standalone 2.33.0
  • Windows 7 x64
  • JRE 7

 Answers

10

Ok, there is a kind of solution, I do not really like it, but it is something instead of nothing.

The idea is to use more smart "sleep", there is a method waitUntil() which takes an anonymous function and timeout in milliseconds. What is does - runs this passed function in loop until timeout hits or your function return True. So you can run something and wait until context is changed:

$this->waitUntil(function () {
    if ($this->byCssSelector('h1')) {
        return true;
    }
    return null;
}, 5000);

I still will be glad if somebody give better solution.

Wednesday, March 31, 2021
 
EurekA
answered 9 Months ago
68

Quick update: phpunit does now support Selenium 2

  • https://phpunit.de/manual/3.6/en/selenium.html
  • https://phpunit.de/manual/4.8/en/selenium.html

At the time of writing, PHPUnit does not support Selenium 2.

php-webdriver from facebook allows the complete WebDriver API to be called from PHP in an elegant way. To quote:

Most clients require you to first read the protocol to see what's possible, then study the client itself to see how to call it. This hopes to eliminate the latter step.

It is used by starting up the Selenium 2 server, which provides the interface at localhost:4444/wd/hub.

/usr/bin/java -jar /path/to/selenium-server-standalone-2.7.0.jar

then running the PHP test code, which calls that interface. For example:

<?php

require '/path/to/php-webdriver/__init__.php';

$webdriver = new WebDriver();

$session = $webdriver->session('opera', array());
$session->open("http://example.com");
$button = $session->element('id', 'my_button_id');
$button->click();
$session->close();

The WebDriver API is mapped to PHP methods, compare calling click on element in the example with the element/click API call in the documentation.

The test code can then be wrapped in regular phpUnit tests.

This is not native phpUnit support, but it's a quite robust approach.

Sunday, August 8, 2021
 
Jauco
answered 4 Months ago
73

The error handling of this is rather poor on phpunit's part; if everything isn't perfect it will silently ignore your other options without a warning.

As Dave mentioned, if any of the variables are misspelled it will silently not work, and you might also try assigning them to the instance in your setUp.

Also, not every condition triggers a screenshot. Try $selenium->assertTextPresent("foobarbaz") instead of your $selenium->fail() for a sanity check.

Wednesday, August 18, 2021
 
Yanick Rochon
answered 4 Months ago
100

As per your statement I would want not to have value in executeScript, but java variable you can reference the Sting value through a Java variable :

String myValue = "80120804076";
WebElement pesel = driver.findElement(fldPesel);
jse.executeScript("arguments[0].value='" + myValue + "';", pesel);
Monday, August 30, 2021
 
ShadowZzz
answered 3 Months ago
95

I had the same problem and I was able to solve it by using the robot, creating function for the url and passing it to a different thread.

    Runnable mlauncher = () -> {
    try {

      driver.get(url);
     } catch (Exception e) {
          e.printStackTrace();
       }
    };

public void myfunction {
 try {

   Thread mthread = new Thread(mlauncher);
   mthread.start

  robot.keyPress(KeyEvent.VK_ENTER);
  robot.keyRelease(KeyEvent.VK_ENTER);

 } catch (Exception e) {
          e.printStackTrace();
       }
Tuesday, September 28, 2021
 
Seibar
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share