0

When I used object repository within base class using page object model the element cannot be identified by the selenium it is returning the invalid selector exception I posted the classes which I executed

Package Failed_Scripts_SignUp;  
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import Object_Repository.Config_Reader;

public class Signup_MethodDriven_Framework {

    WebDriver driver;
    Config_Reader  con_reader=new Config_Reader();

    @Test(priority=1)
    public void Setup() throws InterruptedException {
        System.out.println("launching chrome browser");
        System.setProperty("webdriver.chrome.driver", con_reader.getChromepath()+"chromedriver.exe");
        driver = new ChromeDriver();                            
        driver.manage().timeouts().implicitlyWait(150, TimeUnit.SECONDS);
        driver.navigate().to("http://mobactech.com/tms");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(150, TimeUnit.SECONDS);
        BaseClass_TMS_Signup baseclass=new BaseClass_TMS_Signup(driver);
   driver.findElement(By.className("signup")).click();                                      
                driver.manage().timeouts().implicitlyWait(150, TimeUnit.SECONDS);
                baseclass.enter_username("deepika");
                baseclass.enter_mail("deepika@mobactech.com");
                baseclass.enter_mobile("9876543234");
            }
        }


        // baseclass.java
        package Failed_Scripts_SignUp;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.WebElement;
        import org.openqa.selenium.support.FindBy;
        import org.openqa.selenium.support.PageFactory;
        import org.testng.annotations.AfterClass;
        import Object_Repository.Config_Reader;

        public class BaseClass_TMS_Signup {

             WebDriver driver;

            Config_Reader  con_reader=new Config_Reader();

            public BaseClass_TMS_Signup(WebDriver driver)
            {
             PageFactory.initElements(driver, this);
            }

            @FindBy(xpath="//input[contains(@name,'user_name')]")
            public WebElement username;

            @FindBy(xpath="//input[contains(@name,'user_email')]")
            public WebElement useremail;

            @FindBy(xpath="con_reader.getusermobile()")
            public WebElement usermobile;

            @FindBy(xpath="//input[(@type='checkbox')]")
            public WebElement selectradio;

            @FindBy(xpath="//input[contains(@name,'submit')]")
            public WebElement signup;

                public void enter_username(String num1) {
                username.clear();
                username.sendKeys(num1);
            }

            public void enter_mail(String num2) {
                useremail.clear();
                useremail.sendKeys(num2);
            }

            public void enter_mobile(String num2) {
                usermobile.clear();
                usermobile.sendKeys(num2);

                selectradio.click();
                signup.click(); 
            }

            @AfterClass
            public void close()
            {
                driver.close();
            }
        }

        // objectrepository
        ChromeDriver= D:/admin/
        FirefoxDriver= D:/admin/
        Signup.Tab=//li/a[contains(text(),'Sign up')]
        User.Name=//input[contains(@name,'user_name')]
        User.EmailAddress=//input[contains(@name,'user_email')]
        User.Mobile=//input[contains(@name,'user_mobile')]
        User.AgreeTerms=//input[(@type='checkbox')]
        User.SignUpButton=//input[contains(@name,'submit')]
        SignIn.Tab=//li/a[contains(text(),'Sign in')]
        User.Password=//input[contains(@name,'user_password')]
        User.SignInButton=//input[contains(@name,'submit')]
        URL=http://mobactech.com/tms

       // ConfigReader.java
        package Object_Repository;
        import java.io.File;
        import java.io.FileInputStream;
        import java.util.Properties;

        public class Config_Reader {

            Properties pro;

            public Config_Reader()
            {
                File src=new File("./Configuaration/Config.property");
                try {
                    FileInputStream fis=new FileInputStream(src);

                     pro=new Properties();

                    pro.load(fis);

                } catch (Exception e) {

                    System.out.println("Exception is " +e.getMessage());            
                }       
            }

            public String getChromepath()
            {
                String path=pro.getProperty("ChromeDriver");
                return path;
            }

            public String getURL()
            {
                String url=pro.getProperty("URL");
                return url;
            }

            public String getsignuptab()
            {
                String tab=pro.getProperty("Signup.Tab");
                return tab;
            }

            public String getusername()
            {
                String name=pro.getProperty("User.Name");
                return name;
            }

            public String getuseremail()
            {
                String email=pro.getProperty("User.EmailAddress");
                return email;
            }

            public String getusermobile()
            {
                String mobile=pro.getProperty("User.Mobile");
                return mobile;
            }

            public String getAgree()
            {
                String agree=pro.getProperty("User.AgreeTerms");
                return agree;
            }

            public String getsignupbutton()
            {
                String signup=pro.getProperty("User.SignUpButton");
                return signup;
            }

            public String getsignintab()
            {
                String agree=pro.getProperty("SignIn.Tab");
                return agree;
            }

            public String getpassword()
            {
                String agree=pro.getProperty("User.Password");
                return agree;
            }

            public String getsigninbutton()
            {
                String agree=pro.getProperty("User.SignInButton");
                return agree;
            }   
        }
Assafs
  • 3,257
  • 4
  • 26
  • 39
deepika
  • 1
  • 2

2 Answers2

0

You cannot tell selenium to execute a method to propagate xpath expression to @FindBy annotation parameter. Parameter is just a text and this should be a constant. What you're doing is just say selenium "Hey, give me an element located by the xpath 'con_reader.getusermobile()'". Selenium takes the xpath and obviousely cannot find the element that would match the string 'con_reader.getusermobile()'.

It is really not trivial to parameterize @FindBy. Get more information here: Selenium Page Object Reuse

Alexey R.
  • 8,057
  • 2
  • 11
  • 27
-1

Your xpath selector is invalid for below line @FindBy(xpath="con_reader.getusermobile()"). The one you have used may be CSS selector.

SachinB
  • 348
  • 6
  • 18