Page Object using Page Factory switching driver to i frame

1,455 views
Skip to first unread message

Omair Javaid

unread,
Aug 27, 2015, 8:41:18 AM8/27/15
to Selenium Users
Page Object using Page Factory switching driver to i frame,


I have a class where i have initiated all the methods of xxxxxx page and i want to switch to i frame to send title but with below approach i got null pointer error, please suggest how can i do that 

locator
=====
@FindBys({@FindBy(id = "cke_1_contents"), @FindBy(className = "cke_wysiwyg_frame cke_reset"), @FindBy(css = "[aria-describedby='cke_75']")})
private WebElement slideTitle;

Action
=====
public void setSlideTitle(String slideTitle){
driver.switchTo().frame(submitSlide);
driver.findElement(By.className("cke_wysiwyg_frame cke_reset")).sendKeys(slideTitle);
driver.switchTo().defaultContent();
driver.switchTo().defaultContent();
}

Test Case
========
CoursePage coursePage = PageFactory.initElements(driver, CoursePage.class);

                coursePage.clickAddSlide();
coursePage.setSlideTitle("Title Slide Lesson"); /// not able to locate this since it is under ifram 
coursePage.clickSubmitSlide();

Vladyslav Lopatynskyi

unread,
Aug 27, 2015, 8:54:04 AM8/27/15
to Selenium Users
You need to send your driver instance to a method 
public void setSlideTitle(String slideTitle, WebDriver driver){
driver.switchTo().frame(submitSlide);
driver.findElement(By.className("cke_wysiwyg_frame cke_reset")).sendKeys(slideTitle);
driver.switchTo().defaultContent();
driver.switchTo().defaultContent();
}
And call it from test  coursePage.setSlideTitle("Title Slide Lesson", driver);

But the better approach is to implement Singltone pattern to your page object model

Omair Javaid

unread,
Aug 27, 2015, 9:42:59 AM8/27/15
to Selenium Users
What you Means by (better approach is to implement Singltone pattern to your page object model) 
Did you Mean how i am initiating my page class at test case  

CoursePage coursePage = PageFactory.initElements(driver, CoursePage.class);

If Yes please provide a suggestion 

==========================================================================

I Changed my Code to below but still its unable to locate the element your help is much appropriated, let me add the html as well

Error = org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.chained({By.id: cke_1_contents,By.cssSelector: [ title='Rich Text Editor, name']})

HTML
====
<div id="contentHolder" class="content-holder">
  <div class="modals">
    <div id="addSlideModal" class="modal fade in" aria-hidden="false" aria-labelledby="add-slide-label" role="dialog" tabindex="-1" style="display: block;">
       <div class="modal-dialog modal-lg">
         <div class="modal-content">
           <div class="modal-body">
             <form id="addSlideform" class="form-horizontal" role="form" novalidate="novalidate">
               <input id="isCustomTemplate" type="hidden" value="false" name="isCustomTemplate">
                <div class="form-body">
                 <div class="form-group">
                  <div class="col-md-7">
                  <textarea id="name" class="form-control" rows="4" name="name" data-config="TITLE" style="visibility: hidden; display: none;"></textarea>
                   <div id="cke_name" class="cke_1 cke cke_reset cke_chrome cke_editor_name cke_ltr cke_browser_gecko" lang="en" aria-labelledby="cke_name_arialbl" role="application" dir="ltr">
                   <span id="cke_name_arialbl" class="cke_voice_label">Rich Text Editor, name</span>
                    <div class="cke_inner cke_reset" role="presentation">
                    <span id="cke_1_top" class="cke_top cke_reset_all" style="height: auto; -moz-user-select: none;" role="presentation">
                     <div id="cke_1_contents" class="cke_contents cke_reset" role="presentation" style="height: 100px;">
                     <span id="cke_75" class="cke_voice_label">Press ALT 0 for help</span>
                      <iframe class="cke_wysiwyg_frame cke_reset" frameborder="0" src="" style="width: 100%; height: 100%;" title="Rich Text Editor, name" aria-describedby="cke_75" tabindex="0" allowtransparency="true">
                      <!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<body class="cke_editable cke_editable_themed cke_contents_ltr cke_show_borders" contenteditable="true" spellcheck="false">
</body>
</html>
</iframe>
</div> 


Locator
======
@FindBys({@FindBy(id = "cke_1_contents"), @FindBy(css = "[ title='Rich Text Editor, name']")})
private WebElement slideTitleIFrame;

Action
=====
public void setSlideTitle(String slideTitle,WebDriver driver){
driver.switchTo().frame(this.slideTitleIFrame);
this.slideTitleIFrame.sendKeys(slideTitle);
driver.switchTo().defaultContent();
}

Test Case
========
CoursePage coursePage = PageFactory.initElements(driver, CoursePage.class);

coursePage.setSlideTitle("Title Slide Lesson",driver);

Vladyslav Lopatynskyi

unread,
Aug 27, 2015, 9:58:46 AM8/27/15
to Selenium Users
First of all you wrote that you had nullPointer. So nullPointer gone after you start to send WebDiver object?

Second Part is that FindBy or FindAll annotation return(stores) List of WebElement and not just 1 webElement.
I would suggest you to use xpath in this particular case if you need only 1 element. 

Third. if you are using FindBy annotations you don't need to driver.findElelement in your actions. Just used your variable defined previously.

Singltone
I mean keeping single driver object through the whole project, not only inside testClass. This could be done by static driver + code: 
   private static WebDriver driver = null;
public void instantiateDriver(){
if(driver == null)
return new FireFoxDriver():(or other)
}
return driver;
}

Second approach is to put your driver object on a top class hierarchy of your PageObject model and share it with all your siblings through class hierarchy.

Sonia saini

unread,
Aug 27, 2015, 9:59:12 AM8/27/15
to seleniu...@googlegroups.com
First try to print how many frames you have and then switch.

I was also facing same problem then below code resolved my issue. Try to change a bit.

List<WebElement> iframes = getWebDriver().findElements(By.tagName("iframe"));
LOGGER.info("Size of iframes" + iframes.size());

getWebDriver().switchTo().frame("ampJssdkIframe");

WebElement acceptButton = getWebDriver().findElement(By.xpath("//*[@id='acceptButton']"));
String nameofButton = acceptButton.getText();
LOGGER.info("\n" + nameofButton);
LOGGER.info(acceptButton.isEnabled() + "\n" + acceptButton.getLocation() + "\n"
+ acceptButton.isDisplayed());
acceptButton.click();


getWebDriver().findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
ArrayList<String> tabs = new ArrayList<String>(getWebDriver().getWindowHandles());
getWebDriver().switchTo().window(tabs.get(0));

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/ff3f3405-d6e5-42ac-9961-0b58a18fdf6a%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Sonia saini

Omair Javaid

unread,
Aug 27, 2015, 10:07:11 AM8/27/15
to Selenium Users
Yes thats right null pointer is gone but now locator is not being found ?

Secondly it is returning 1 Webelement with @FindBys() notation unfortunately i am not allowed to use X path as it is prohibited by my director so i have to do some thing with same 

Vladyslav Lopatynskyi

unread,
Aug 27, 2015, 4:39:19 PM8/27/15
to Selenium Users
Change your locator directly in method cause you are not using your WebElement variable
driver.findElement(By.id("cke_1_contents")).sendKeys(slideTitle);

Omair Javaid

unread,
Aug 28, 2015, 3:54:10 AM8/28/15
to Selenium Users
Hi,

Firstly i would like to thank you for helping me out,

I am using my Web Element like below first time it worked now its gives me following error: please help its getting frustratednow

1. All i need to do is get the element of this i frame send keys and go on ....

org.openqa.selenium.WebDriverException: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMWindowUtils.sendKeyEvent]

WebElement
@FindBys({@FindBy(id = "cke_1_contents"), @FindBy(tagName = "iframe")})
private WebElement slideTitleIFrame; 

Action 

public void setSlideTitle(String slideTitle, WebDriver driver){
driver.switchTo().frame(slideTitleIFrame);
((JavascriptExecutor) driver).executeScript("document.body.innerHTML = '<br>'");
WebElement bodyOfMail = driver.switchTo().activeElement();
bodyOfMail.sendKeys("Your mail body content here");
driver.switchTo().defaultContent();
}
Reply all
Reply to author
Forward
0 new messages