【问题标题】:Getting "java.lang.OutOfMemoryError" exception while running a cucumber test using java-selenium使用 java-selenium 运行黄瓜测试时出现“java.lang.OutOfMemoryError”异常
【发布时间】:2021-12-04 22:09:48
【问题描述】:

使用 java-selenium 运行黄瓜测试时,在以下行出现错误:

PageFactory.initElements(driver,LandingPage.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();
                
    }

}

下面是步骤定义文件代码:

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();
    }

}

出现以下错误:

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)

注意:我已尝试增加 java 堆内存,但仍然出现相同的错误,可能的解决方案是什么。

【问题讨论】:

  • stacktrace 的根是什么?

标签: java selenium cucumber out-of-memory java-heap


【解决方案1】:

我终于可以解决这个问题了。下面是正确的代码。我已将 PageFactory.initElements(driver,LandingPage.class); 更改为 PageFactory.initElements(driver,this); 并开始正常工作。

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();
                
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多