javaappiumappium-android

Appium Page Object Model return Garbage Collected


I'm pretty new to Appium. I currently follow some video to create a POM Appium android testing. When i try to run the test, it throws this error:

java.lang.IllegalStateException: The element By.id: nameText is not locatable anymore because its context has been garbage collected

for more information, here I attached my Page Object class:

public class LoginPage extends AndroidGestures {
    
    AndroidDriver driver;
    
    public LoginPage(AndroidDriver driver) {
        super(driver);
        this.driver = driver;
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }
    
    @AndroidFindBy(id="com.androidsample.generalstore:id/nameField")
    private WebElement nameText;
    
    @AndroidFindBy(id="com.androidsample.generalstore:id/radioMale")
    private WebElement genderMale;
    
    @AndroidFindBy(id="com.androidsample.generalstore:id/radioFemale")
    private WebElement genderFemale;
    
    @AndroidFindBy(id="com.androidsample.generalstore:id/btnLetsShop")
    private WebElement loginButton;
    
    @AndroidFindBy(id="android:id/text1")
    private WebElement country;
    
    public void setName(String name) {
        nameText.sendKeys(name);
        driver.hideKeyboard();
    }
    
    public void setGender(String gender) {
        if(gender.toLowerCase().equals("female")) {
            genderFemale.click();
        } else if (gender.toLowerCase().equals("male")) {
            genderMale.click();
        }
    }
    
    public void selectCountry(String cntr) {
        country.click();
        scrollDownToTextAndClick(cntr);
    }
    
    public void login() {
        loginButton.click();
    }

}

Test class:

public class BasicStore extends BaseClass {
    
    LoginPage lgp = new LoginPage(driver);
    
    @Test (priority = 1, description = "Enter the general store")
    public void POM001() {
        
        // filling the form
        lgp.setName("Ayumi"); --> I assume this is where things go wrong
        lgp.setGender("female");
        lgp.selectCountry("Bahamas");
        lgp.login();
        
        threadSleep(3);
        
        // assert that we enter the correct page
        Boolean page = driver.findElement(AppiumBy.id("com.androidsample.generalstore:id/appbar_btn_cart")).isDisplayed();
            Assert.assertTrue(page);
    }
}

Some of the things i did try:

As I've been looking around and haven't found anything related to this (or i was just too impatient), 2 questions pops out:

Many thanks in advance for the time and answer!


Solution

  • In test class, try to put LoginPage lgp = new LoginPage(driver); inside the public void POM001() {}, then it should work

    public class BasicStore extends BaseClass {
        @Test (priority = 1, description = "Enter the general store")
        public void POM001() {
            LoginPage lgp = new LoginPage(driver);
            // filling the form
            lgp.setName("Ayumi"); --> I assume this is where things go wrong
            lgp.setGender("female");
            lgp.selectCountry("Bahamas");
            lgp.login();
            
            threadSleep(3);
            
            // assert that we enter the correct page
            Boolean page = driver.findElement(AppiumBy.id("com.androidsample.generalstore:id/appbar_btn_cart")).isDisplayed();
                Assert.assertTrue(page);
        }
    }