Element located when put in a single test case but not located when put in separate test case



  • 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; 
    }
    


  • 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.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2