简体   繁体   中英

Getting "java.lang.OutOfMemoryError" exception while running a cucumber test using java-selenium

While running a cucumber test using java-selenium getting error at following line:

PageFactory.initElements(driver,LandingPage.class);

Below is the complete code of Page Object Class:

public class LandingPage {
    
    WebDriver driver;
    
    public LandingPage(WebDriver driver)
    {
        this.driver=driver;
        PageFactory.initElements(driver,LandingPage.class);
    }
    
    @FindBy(xpath="//a[contains(@id,'accountList')]")
    WebElement account_dd;
    
    public void movemousetodropdown() {
        
        System.out.println("I am trying to move mouse");
        Actions act = new Actions(driver);
        act.moveToElement(account_dd).build().perform();
                
    }

}

Below is the step definition file code:

package stepsdefinition;

import java.time.Duration;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import pages.LandingPage;
import utilities.ConfigReader;

public class AmazonDropdownTest1 {
        
    public static WebDriver driver;
    LandingPage p1;
    @Given("I am on amazon dot com")
    public void i_am_on_amazon_dot_com() {
        
        // Write code here that turns the phrase above into concrete actions
        
        System.setProperty("webdriver.chrome.driver", ConfigReader.getInstance().getChromePath());
        driver = new ChromeDriver();
        driver.get("https://amazon.com");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        System.out.println("step1");
        p1= new LandingPage(driver);
    }
    @When("I take my mouse to the first dropdown")
    public void i_take_my_mouse_to_the_first_dropdown() {
        // Write code here that turns the phrase above into concrete actions
         System.out.println("step2");
        p1.movemousetodropdown();
       
    }
    @When("I click on the first dropdown")
    public void i_click_on_the_first_dropdown() {
        // Write code here that turns the phrase above into concrete actions
         System.out.println("step3");
    }
    @Then("I am able to see list of values on dropdown")
    public void i_am_able_to_see_list_of_values_on_dropdown() {
        // Write code here that turns the phrase above into concrete actions
         System.out.println("step4");
    }
    @Then("I click one of the option from that dropdown")
    public void i_click_one_of_the_option_from_that_dropdown() {
        // Write code here that turns the phrase above into concrete actions
         System.out.println("step5");
        
        driver.quit();
    }

}

Getting following errors:

java.lang.OutOfMemoryError: Java heap space
    at java.base/java.util.Arrays.copyOf(Arrays.java:3744)
    at java.base/java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:146)
    at java.base/java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:607)
    at java.base/java.lang.StringBuffer.append(StringBuffer.java:355)
    at java.base/java.io.StringWriter.write(StringWriter.java:122)
    at java.base/java.io.PrintWriter.write(PrintWriter.java:542)
    at java.base/java.io.PrintWriter.write(PrintWriter.java:559)
    at java.base/java.io.PrintWriter.print(PrintWriter.java:686)
    at java.base/java.io.PrintWriter.println(PrintWriter.java:839)
    at java.base/java.lang.Throwable$WrappedPrintWriter.println(Throwable.java:768)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:701)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)
    at java.base/java.lang.Throwable.printEnclosedStackTrace(Throwable.java:713)

Note: I have tried to increase java heap memory but still getting the same error, what could be the possible solution.

Finally I am able to solve this. Below is the correct code. I have changed PageFactory.initElements(driver,LandingPage.class); to PageFactory.initElements(driver,this); and it started working fine.

package pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LandingPage {
    
    WebDriver driver;
    
    public LandingPage(WebDriver driver)
    {
        this.driver=driver;
        PageFactory.initElements(driver,this);
    }
    
    @FindBy(xpath="//a[contains(@id,'accountList')]")
    WebElement account_dd;
    
    public void movemousetodropdown() {
        
        System.out.println("I am trying to move mouse");
        Actions act = new Actions(driver);
        act.moveToElement(account_dd).build().perform();
                
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM