switchto.frame(ID) not working for iframe using HtmlUnitDriver

834 views
Skip to first unread message

aadi

unread,
Aug 31, 2010, 7:41:09 AM8/31/10
to webdriver
Hi!

I am facing problems in switching to iframes using HtmlUnitDriver. I
call switchTo.frame() using the id of the iframe. but all assertions I
run for elements inside the iframe after the switch call fail

Assert.assertTrue("Dashboard is present",
selenium.isElementPresent("//div[@id='userservices']"));

here's the code

@Before
public void setup() throws Exception {
webDriver = new HtmlUnitDriver();
final String baseUrl = "http://some-environment/wportal/uss/main?
cc=gb&lc=en";
webDriver.get(baseUrl);
}


@Test
public void testUssSanity() throws Exception {
System.out.println(webDriver.getCurrentUrl());
((HtmlUnitDriver) webDriver).setJavascriptEnabled(true);
final Selenium selenium = new WebDriverBackedSelenium(webDriver,
webDriver.getCurrentUrl());
String thisWindow = webDriver.getWindowHandle();
System.out.println(thisWindow);
webDriver.switchTo().frame("uss_iframe");
System.out.println(webDriver.getWindowHandle());

the output I get is

http://some-environment/wportal/uss/main?cc=gb&lc=en
webdriver1283253044070
webdriver1283253044070

does that mean HtmlUnitDriver is not capable of switching? the test
runs successfully in FirefoxDriver but I have to run them as a batch
job for post deploys testing on our CI environment where I cannot
enable a window environment (for commercial / political reasons).

aadi

unread,
Aug 31, 2010, 7:53:32 AM8/31/10
to webdriver
Hi!

Just checked the output with Firefox deriver too; I had to remove the
method for enabling java script; but basically there everything works;
even though the value of window handles is the same as in
HtmlUnitDriver case. a bit confused now on where I am going wrong. the
div id I use exists within the page. but it still fails for one driver
and works for the other using vanilla xpath.

Output for firefox deriver

http://some-environment/wportal/uss/main?cc=gb&lc=en
{2e11fd5e-26fa-4b03-a975-c20bd6216e7d}
{2e11fd5e-26fa-4b03-a975-c20bd6216e7d}

Eran M.

unread,
Aug 31, 2010, 2:37:36 PM8/31/10
to webd...@googlegroups.com
Hi,

The two things are not the same - since with Firefox, the Javascript is enabled as you load the page. With HtmlUnitDriver, you enable javascript *after* loading the page. Try using HtmlUnitDrivers's constructor that accepts a boolean to enable Javascript. Can you provide a dump of the DOM when you cannot find the element?

Regards,
Eran

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


aadi

unread,
Sep 1, 2010, 5:41:14 AM9/1/10
to webdriver
Hi!

i'll explain the flow a bit. this page where we call a iframe is
session controlled. so when the user initially visits the URL and he
is not logged in he is redirected to different application where he
logs in (OAuth) and is then redirected to the application under test's
home page; where post login we expect an iFrame with some controls. if
I enable java script for the HTML Unit driver it fails at a stage when
it is fetching this login page (the hostname changes to trunk-
nightly.us2.XXX.com).

org.openqa.selenium.WebDriverException:
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot call
method "removeChild" of null (http://trunk-nightly.us2.XXX.com/XX/js/
neo/core.js#22)
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version:
'6.1', java.version: '1.6.0_20'
Driver info: driver.version: htmlunit
at
org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:
280)
at
org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:
262)
at com.foo.bar.tests.TestUSSSanity.setup(TestUSSSanity.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod
$1.runReflectiveCall(FrameworkMethod.java:44)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:
15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:
41)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:
27)
at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:
31)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:
76)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:
50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:
46)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:
38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:
390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
197)
Caused by: com.gargoylesoftware.htmlunit.ScriptException: TypeError:
Cannot call method "removeChild" of null (http://trunk-
nightly.us2.XXX.com/XX/js/neo/core.js#22)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine
$HtmlUnitContextAction.run(JavaScriptEngine.java:527)
at
net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:
537)
at
net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:
538)
at
com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:
457)
at
com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:
910)
at
com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventListeners(EventListenersContainer.java:
159)
at
com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeListeners(EventListenersContainer.java:
273)
at
com.gargoylesoftware.htmlunit.javascript.host.Node.executeEvent(Node.java:
574)
at
com.gargoylesoftware.htmlunit.html.HtmlScript.setAndExecuteReadyState(HtmlScript.java:
470)
at
com.gargoylesoftware.htmlunit.html.HtmlPage.setReadyStateOnDeferredScriptsIfNeeded(HtmlPage.java:
1326)
at
com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:
203)
at
com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:
427)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:
303)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:
360)
at
org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:
274)
... 25 more
Caused by: net.sourceforge.htmlunit.corejs.javascript.EcmaError:
TypeError: Cannot call method "removeChild" of null (http://trunk-
nightly.us2.XXX.com/XXX/js/neo/core.js#22)
at
net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:
3753)
at
net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:
3731)
at
net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:
3759)
at
net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:
3778)
at
net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.undefCallError(ScriptRuntime.java:
3797)
at
net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:
2317)
at
net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:
2310)
at
net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:
1515)
at
net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:
845)
at
net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:
164)
at
net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:
429)
at
com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:
264)
at
net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:
3157)
at
net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:
162)
at
com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:
485)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine
$4.doRun(JavaScriptEngine.java:450)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine
$HtmlUnitContextAction.run(JavaScriptEngine.java:521)
... 39 more

here's the setup method

@Before
public void setup() throws Exception {
webDriver = new HtmlUnitDriver(true);
final String baseUrl = "http://some-environment/wportal/uss/main?
cc=gb&lc=en";
webDriver.get(baseUrl);

webDriver.findElement(By.id("j_username")).sendKeys("anadi...@foo.com");
WebElement password = webDriver.findElement(By.id("j_password"));
password.sendKeys("pass@word1");
password.submit();
selenium = new WebDriverBackedSelenium(webDriver,
webDriver.getCurrentUrl());
}

Disabling the java script I can correctly login through the
redirection and I also reach back to our app's home page. but I am not
able to switch to the iframe then.

I added this assertion after the login to confirm whether the iframe
is present or not:

Assert.assertTrue("Mobical Iframe is present",
selenium.isElementPresent("xpath=id('uss_iframe')"));

this call passes the assertion. and the switch fails

webDriver.switchTo().frame("uss_iframe");
webDriver.findElement(By.id("userservices"));



On Aug 31, 8:37 pm, "Eran M." <eran....@gmail.com> wrote:
> Hi,
>
> The two things are not the same - since with Firefox, the Javascript is
> enabled as you load the page. With HtmlUnitDriver, you enable javascript
> *after* loading the page. Try using HtmlUnitDrivers's constructor that
> accepts a boolean to enable Javascript. Can you provide a dump of the DOM
> when you cannot find the element?
>
> Regards,
> Eran
>
> > webdriver+...@googlegroups.com<webdriver%2Bunsu...@googlegroups.com>
> > .

Eran M.

unread,
Sep 19, 2010, 5:22:17 PM9/19/10
to webd...@googlegroups.com
Hi,

It seems like the Javascript engine of HtmlUnit does not like your Javascript code:
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot call
method "removeChild" of null (http://trunk-nightly.us2.XXX.com/XX/js/
neo/core.js#22
)

Have you checked that code? Is  there a real bug there?
If not, I suggest submitting the javascript snippet together with the error to the HtmlUnit team.

In addition, there was one issue Jason pointed out in another thread - switchTo() can only be used to dig further into the iframes hierarchy. You should switchTo().defaultContent() and then switch to another subframe.

Eran

To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages