Alert alert=driver.switchTo().alert(); Is not Working with test NG

1,901 views
Skip to first unread message

MonikaY

unread,
Sep 18, 2012, 5:33:45 AM9/18/12
to seleniu...@googlegroups.com
Hi...

In my test class I am using selenium webdriver + testng + page Objects + IE browser
Now there is mandatory field check. on Clicking the "Next" button. a pop up message is shown "Lastname is mandatory." with button "ok". I have made two test classes say "home" and "HomeTest"

HomeTest.java contains

public class HomeTest {
    private WebDriver driver;

   
    @BeforeClass
    public void initWebDriver() {
    driver= new InternetExplorerDriver();
     
    }
   
@Test
    public void testGetErrorMesssage() throws Exception{
        Home home=new Home(driver);
        home.getErrorMessage();
    }


Home.java contains


@FindBy(name="button")
private RenderedWebElement next_button;
    
public Home(WebDriver driver) {
           driver.get("http://application.com");
           PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this);
        }

 public void getErrorMessage()throws Exception{
     next_button.click();
     Alert alert=driver.switchTo().alert();
     System.out.println(alert.getText());
     alert.accept();
     }

But my problem is that Eclipse is showing an error line on "alert()" on line "Alert alert=driver.switchTo().alert();" in Home.java
when I mouse over and red line it is showing "The method alert() is undefined for the type WebDriver.TargetLocator"

I don't understand why is this happening :(  ...and this is working fine when I am not using TestNG

Thanks


Krishnan Mahadevan

unread,
Sep 18, 2012, 5:43:41 AM9/18/12
to seleniu...@googlegroups.com
What version of Selenium are you working with ?

I am seeing a reference to :  RenderedWebElement   which makes me believe that you might be working with an outdated version of Selenium.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"






--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/VKZhjLZRs8gJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Message has been deleted
Message has been deleted

Krishnan Mahadevan

unread,
Sep 18, 2012, 5:47:16 AM9/18/12
to seleniu...@googlegroups.com
Selenium 2 ?? Do you mind stating the actual version of Selenium 2 that you are using ? The latest released version of Selenium is Selenium 2.25.0

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



On Tue, Sep 18, 2012 at 3:16 PM, Manoj Hans <hman...@gmail.com> wrote:
try this org.openqa.selenium.Alert instead of Alert
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/nc_FOpQE2CkJ.

MonikaY

unread,
Sep 18, 2012, 5:48:15 AM9/18/12
to seleniu...@googlegroups.com

I am using selenium webdriver ..............

Krishnan Mahadevan

unread,
Sep 18, 2012, 5:50:11 AM9/18/12
to seleniu...@googlegroups.com
Sigh !! I give up !

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/ufxvYPE1IdEJ.

Manoj Hans

unread,
Sep 18, 2012, 5:55:00 AM9/18/12
to seleniu...@googlegroups.com
Krishnan asking which version of selenium 2 u r using??

like 2.20....

MonikaY

unread,
Sep 18, 2012, 5:57:16 AM9/18/12
to seleniu...@googlegroups.com
Yes it is "Selenium 2.25.0"

I have used "org.openqa.selenium.Alert alert=driver.switchTo().alert();"
but still getting the error line...........

Manoj Hans

unread,
Sep 18, 2012, 6:08:21 AM9/18/12
to seleniu...@googlegroups.com
can u post complete code of both the class???

Neeraj Sharma

unread,
Sep 18, 2012, 6:39:53 AM9/18/12
to seleniu...@googlegroups.com
Hi Monika, 
 I think try to declare the alert outside the method, may be it can resolve your solution.

MonikaY

unread,
Sep 18, 2012, 7:05:06 AM9/18/12
to seleniu...@googlegroups.com
Manoj.....

I have implemented  as in http://zloiadun.blogspot.in/2010/08/using-page-object-pattern-for-web.html

import org.openqa.selenium.RenderedWebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;


public class Home {
    private WebDriver driver;


    @FindBy(name="button")
     private RenderedWebElement next_button;
     
     public Home(WebDriver driver) {
           driver.get("http://application.com");
           PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this);
        }

public void getErrorMessage()throws Exception{
     next_button.click();
     Alert alert=driver.switchTo().alert();
     System.out.println(alert.getText());
     alert.accept();
     }


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

Neeraj Sharma

unread,
Sep 18, 2012, 8:01:21 AM9/18/12
to seleniu...@googlegroups.com
Hi Monika, I tried to automate a alert popup window to solve your problem and this code is working fine for me,

public class HomeTest {
private static WebDriver driver;

    
    @Before
   
    public  void initWebDriver() {
    driver= new InternetExplorerDriver();
      
    }
    
@Test
    public void testGetErrorMesssage() throws Exception{
        Home home=new Home(driver);
        home.getErrorMessage(driver);
    }
}

and Home class is as:-



public class Home {
private WebDriver driver;

@FindBy(xpath="//div[3]/p[2]/input")
private WebElement next_button;
    
public Home(WebDriver driver) throws InterruptedException {
          Thread.sleep(1000);
          PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this);
       }


public void getErrorMessage(WebDriver driver)throws Exception{
      next_button.click();
      Alert alert=driver.switchTo().alert();
  System.out.println(alert.getText());
      alert.accept();
}

}

I have just changed the @beforeclass to @Before and passing the driver in the getErrorMessage method as your driver is not passing in your method.
Please have a look into this and let us know if your problem is solved or still facing the same issue.

Thanks,
Neeraj Sharma
Paxcel Technologies Pvt Ltd.

Manoj Hans

unread,
Sep 18, 2012, 8:06:11 AM9/18/12
to seleniu...@googlegroups.com
try this.....

package com;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;


public class Home {
    private WebDriver driver;


    @FindBy(name="button")
     private WebElement next_button;
     
     public Home(WebDriver driver) {
           driver.get("http://application.com");
           PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this);
        }

public void getErrorMessage()throws Exception{
     next_button.click();
     Alert alert=driver.switchTo().alert();
     System.out.println(alert.getText());
     alert.accept();
}}

-----------------------------------------------------------------------

package com;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


public class HomeTest {
    private WebDriver driver;

    
    @BeforeClass
    public void initWebDriver() {
       driver= new InternetExplorerDriver();
    }

    @Test
    public void testGetErrorMesssage() throws Exception{
        Home home=new Home(driver);
        home.getErrorMessage();
    }
}


--Manoj Hans

Neeraj Sharma

unread,
Sep 18, 2012, 8:16:21 AM9/18/12
to seleniu...@googlegroups.com
@Manoj  can we use the @beforeclass for initializing the driver as it runs before the class so i think it may display a error message too.

Manoj Hans

unread,
Sep 18, 2012, 8:17:48 AM9/18/12
to seleniu...@googlegroups.com
@Neeraj...no need to pass webdriver in getErrorMessage method  becoz u already declared Webdriver globally.

Neeraj Sharma

unread,
Sep 18, 2012, 8:20:10 AM9/18/12
to seleniu...@googlegroups.com
Yes,I have tried this but my code is not running after calling the Home class constructor that means the driver is not found by the getErrorMessage method. So I have to pass the driver in it.

Manoj Hans

unread,
Sep 18, 2012, 8:20:40 AM9/18/12
to seleniu...@googlegroups.com
@Neeraj...take both classes as it is which i posted earlier it won't show any error....

Neeraj Sharma

unread,
Sep 18, 2012, 10:28:03 AM9/18/12
to seleniu...@googlegroups.com
Hi Manoj, 

I have tried your code as it is and just modify the URL and the xpath of the button. But what I have found that driver is able to click on the button but after that it display the java null pointer exception for the line Alert alert=driver.switchTo().alert(); and if I pass the driver in the method then it works fine. What is the main reason behind this? Same for my code if i pass the driver in getErrorMessage then it works fine but if I remove it it only runs till clicking on the button but not able to switch to the alert window. 

Neeraj Sharma.

Manoj Hans

unread,
Sep 18, 2012, 12:35:56 PM9/18/12
to seleniu...@googlegroups.com
Ya u r right....need to pass webdriver in the same method

Neeraj Sharma

unread,
Sep 19, 2012, 12:59:31 AM9/19/12
to seleniu...@googlegroups.com
@Monika, have your problem resolved or not?

MonikaY

unread,
Sep 19, 2012, 1:25:38 AM9/19/12
to seleniu...@googlegroups.com
I was making two mistakes:

1) I was using "RenderedWebElement" which is outdated version of Selenium and also including selenium server jar with Selenium 2.25 jar also . Now when I removed "selenium server jar" jar ...the red line under "alert()" is gone .....because of this reason my test case was not running in Firefox and was showing an exception..Now the test case is also running with Mozilla firefox too...Thanks Krishnan :)

2)
Secondly, the driver was not found by the getErrorMessage method. So I need to pass webdriver in the same method...Thanks Neeraj and Manoj

Neeraj Sharma

unread,
Sep 19, 2012, 1:27:29 AM9/19/12
to seleniu...@googlegroups.com
@Manoj, I have again checked the code and found that in the Home class constructor we are defining the driver equal to the static driver, so that why we have to pass the driver in the method,
here is the code :-

public Home(WebDriver driver) throws InterruptedException {
this.driver=driver;
          Thread.sleep(1000);
          PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this);
       }

Neeraj Sharma

unread,
Sep 19, 2012, 1:34:57 AM9/19/12
to seleniu...@googlegroups.com

On Wednesday, 19 September 2012 10:55:38 UTC+5:30, MonikaY wrote:
I was making two mistakes:

1) I was using "RenderedWebElement" which is outdated version of Selenium and also including selenium server jar with Selenium 2.25 jar also . Now when I removed "selenium server jar" jar ...the red line under "alert()" is gone .....because of this reason my test case was not running in Firefox and was showing an exception..Now the test case is also running with Mozilla firefox too...Thanks Krishnan :)

2)
Secondly, the driver was not found by the getErrorMessage method. So I need to pass webdriver in the same method...Thanks Neeraj and Manoj
We are not passing the static driver in the constructor so that's why we have to pass the driver in the method:D

Krishnan Mahadevan

unread,
Sep 19, 2012, 1:46:09 AM9/19/12
to seleniu...@googlegroups.com
Monika,
Am glad you figured out what I was hinting at :) Reference to RenderedWebElement in your sample clue was the clue :)

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/SQ2TELKgWOMJ.
Reply all
Reply to author
Forward
0 new messages