Skip to content

Instantly share code, notes, and snippets.

@ypresto
Created December 16, 2012 15:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ypresto/4308281 to your computer and use it in GitHub Desktop.
Save ypresto/4308281 to your computer and use it in GitHub Desktop.
casperjs iframe test example
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<iframe name="ad_iframe" src="./casper_test.html" />
</body>
</html>
/*global require:false */
var casper = require('casper').create();
casper.start('./iframe_test.html');
casper.withFrame('ad_iframe', function () {
this.test.assertUrlMatch(/casper_test\.html/, 'i am on inside of iframe');
this.test.assertVisible('#hoge', 'hoge is clickable');
this.clickLabel('clickable hoge');
});
casper.withFrame('ad_iframe', function () {
this.test.assertUrlMatch(/#!\/hoge/, 'hash changed to hoge');
this.test.assertNotVisible('#fuga', 'fuga is not clickable');
this.clickLabel('unclickable fuga');
});
casper.withFrame('ad_iframe', function () {
this.test.assertUrlMatch(/#!\/hoge/, 'hash not changed to fuga');
this.echo("I'm sorry, it's clickable...");
});
casper.run();
@bikong0411
Copy link

if iframe no name, what can i do?

@pbojinov
Copy link

I have the same problem trying to select an iframe with no name. Any suggestions?

@hustcer
Copy link

hustcer commented Oct 21, 2014

I have the same problem too, any ideas? many thanks.

@kelu27
Copy link

kelu27 commented Nov 14, 2014

Use casper.page.switchToChildFrame(0) for the first iframe

@AkaAnto
Copy link

AkaAnto commented Jul 1, 2015

You can call withFrame with an index for those iframes with no name attribute:

casper.withFrame(0, function() {
this.test.assertTitle('FRAME TITLE');
});

You just has to figure out the index of your desired iframe. I usually do it by inspecting the html code of the page.

@eclair04
Copy link

eclair04 commented Jun 22, 2017

Is there any way we can fill up a form inside casper.withFrame() for example this.sendKeys() or this.fill() or this.fillSelectors()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment