0

I am in a weird situation where I found that my element is located when I wrote in a single test case. But as soon as I put that element in a separate test case, Unable to locate element Exception is been given.

This is the code which is not working.

@Test
public void login() throws Exception {


    getdriver().get("http://10.6.6.132:3000/login");

    EyeGuide_Login_Pom lg=PageFactory.initElements(getdriver(), 
    EyeGuide_Login_Pom.class);
    lg.loginEyeguide();
    Thread.sleep(5000);
    Assert.assertEquals(getdriver().getTitle(),"EyeGuide");
    //Assert.assertEquals(getdriver().getCurrentUrl(),lg.url);
    Thread.sleep(2000);
}

@Test
public void deactivateorg() throws Exception {
    organizationpagepom or=PageFactory.initElements(getdriver(), organizationpagepom.class);
    or.deactorg();
}

}

This is the code which works as no separate test case is added.

@Test
public void login() throws Exception {


    getdriver().get("http://10.6.6.132:3000/login");

EyeGuide_Login_Pom lg=PageFactory.initElements(getdriver(), 
    EyeGuide_Login_Pom.class);
lg.loginEyeguide();
Thread.sleep(5000);
Assert.assertEquals(getdriver().getTitle(),"EyeGuide");
//Assert.assertEquals(getdriver().getCurrentUrl(),lg.url);
Thread.sleep(2000);
}


    organizationpagepom or=PageFactory.initElements(getdriver(), 
    organizationpagepom.class);
or.deactorg();


}

Below is the pom I am using.

public class organizationpagepom extends Basetest  {
//  organizationpagepom(WebDriver driver){
//      super(driver);
//  }
@FindBy(css="a[href='/organization']")
WebElement manageorg;


@FindAll(@FindBy(xpath="//*[text()='Deactivate']"))
List<WebElement> deactorglinks;
@FindAll(@FindBy(xpath="//*[@class='ant-btn downloadBtn ant-dropdown- 
    trigger']"))
List<WebElement> actdeactseldrpdown;
@FindAll(@FindBy(xpath="//*[@class='ant-dropdown-menu-item']"))
List<WebElement> activeinactiveddownvalues;


public void deactorg() throws Exception
{
    manageorg.click();
    Thread.sleep(2000);
    deactorglinks.get(0).click();
    Thread.sleep(2000);
    actdeactseldrpdown.get(1).click();
    Thread.sleep(2000);
    activeinactiveddownvalues.get(0).click();
    Thread.sleep(3000);
}

    }

EyeGuide_Login_Pom code is as follows.

public class EyeGuide_Login_Pom {



    @FindBy(name="phoneInput")
    WebElement un;
    @FindBy(xpath="//*[@placeholder='Password']")
    WebElement pw;
    @FindBy(css="button[type='button']")
    WebElement signinbtn;


    public void loginEyeguide() 
    {
        un.clear();
        un.sendKeys("tdladmin");
        pw.sendKeys("super");
        signinbtn.click();

    }



}

I have taken the WebDriver object as private and I used getdriver() method to access the private variable. I don't know why this issue is been popped out. The code of WebDriver access is as follows.

public class Basetest {
    private WebDriver driver;


    @BeforeTest
    public void setbrowserproperties()
    {
        System.setProperty("webdriver.chrome.driver","F:\\Browser Chrome Driver files\\chromedriver.exe");
        this.driver=new ChromeDriver();
        getdriver().manage().window().maximize();
    }

    @AfterTest
    public void closedriver()
    {
        getdriver().close();
    }
    protected WebDriver getdriver() {
        return driver;
    }

}

Please help me as I am not able to find any solution. Below is the error that I am getting.

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"a[href='/organization']"}

Update: I also tried this but it didn't work and didn't give me any failure.

public organizationpagepom oged() throws Exception {    
    //getdriver().manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    organizationpagepom oot=PageFactory.initElements(getdriver(), organizationpagepom.class); 
    oot.deactorg(); 
    return oot; 
}
  • Pratik can you also add the code of EyeGuide_Login_Pom class code to see if there is anything interrelated. – Ranjit Singh Saluja Oct 1 at 10:02
  • I have added the EyeGuide_Login_Pom class code as requested by you. – Pratik Pathare Oct 1 at 10:20
  • So, what your code do is login and then after login some page comes in which u do something using deactorg() method, Few questions: 1. Is it mandatory to make it [deactorg()] another test case. 2. Why don't you take the return type of login method as your landing page. – Ranjit Singh Saluja Oct 1 at 10:27
  • Didn't get the second question. Can you please elaborate. – Pratik Pathare Oct 1 at 10:45
  • About the first question : Yes. It is mandatory to make deactorg() as another test case as that is my scenario according to my requirements. It represents a particular functionality on the web page which was to be automated. – Pratik Pathare Oct 1 at 10:48
0

I assume (since you haven't provided the context) that your driver is re-created in @Before at the beginning of each test. So that getDriver() of one test returns not the same instance than getDriver() in your second test.

  • Sorry I didn't get you. Can you please elaborate. – Pratik Pathare Oct 1 at 11:38
  • Any solution you can suggest for this ? – Pratik Pathare Oct 1 at 11:39
  • When you take current driver through your getDriver() method that driver has to be initiated somewhere. Usually there are dedicate method which set up preconditions before each test run. In that setup drivers are usually configured. The solution is to rework the method that sets up preconditions so that it would open the initial page both the tests would work in. Do not open the initial page in the test method. – wec Oct 1 at 11:41
  • I have put the code from where I am taking the WebDriver object. Looking at that can you highlight my mistake ? – Pratik Pathare Oct 1 at 12:04
0

Is the element you're looking for only accessible once you've logged in?

If so, in your original test, you logged in and then located the element.

Whereas in your split test, you don't login (because you have that in another test).

Therefore, this may be why you're not locating the element.

  • That does not make a point to log in and logout a number of times to execute test cases if we bind everything in the suite. So I think that cannot be the root cause. Because without pom I tried to run all this in one of my earlier projects. Everything ran properly there. – Pratik Pathare Oct 1 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.