Stephan Kulla
Stephan Kulla

Reputation: 5077

Switching between frames in Selenium 2 with PHP-Webdriver

I currently use Selenium 2 with a local Selenium web server and the PHP-Webdriver written by Facebook.

Now I want to write an automated test for the Facebook Like Button. Because this Button is loaded through an iframe, I first select this frame via $driver->frame(array('id' => 1)) (I Found out, that Facebook loads normally two frames and the second frame is the Like Button). After clicking the Like Button a new Frame is loaded, where the user also can send a comment to his wall. Unfortunately the focus is still on the Like Button Frame so that I have to switch to the second frame. How can I do this?

Because I do not use Selenium RC there is no Selenium.SelectFrame("relative=top") method. I also cannot use the method driver.switchTo().defaultContent() because I do not use the Java webdriver. It seems that I just can use methods specified in the JsonWireProtocol. How can I switch between frames or change the focus back to the top frame?

Upvotes: 2

Views: 4229

Answers (2)

Dustin
Dustin

Reputation: 11

I had this same problem. The issue I discovered, that the "frame" in the PHP webdrivers only switches to frames below the current one. So if you want to switch to a different frame that is above the current frame, you are out of luck. I had to select the main window, which essentially reset me to the top frame. From there I was able to select the correct frame.

//putting a sleep so the page can load
sleep(SLEEPTIME + 5);
//getting a list of windows on the page
$windows = $webdriver->getWindows(); // function below

//switching to the 
$webdriver->selectWindow($windows[0]);

$webdriver->focusFrame('cpAppFrame');

public function getWindows() {
        $request = $this->requestURL . "/window_handles";
        $response = $this->execute_rest_request_GET($request);
        return $this->extractValueFromJsonResponse($response);
    }

Upvotes: 1

Misha Akovantsev
Misha Akovantsev

Reputation: 1825

Python webdriver client has these methods for switching between iframes, windows and pop ups:

switch_to_active_element
switch_to_alert
switch_to_default_content
switch_to_frame
switch_to_window 

switch_to_default_content is the one you need. Find its analog for php client.


UPDATE: Since you mentioned JasonWireProtocol:

http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/frame

POST /session/:sessionId/frame

Change focus to another frame on the page.
If the frame ID is null, the server should switch to the page's default content.

Upvotes: 2

Related Questions