首页 > 解决方案 > 通过 Selenium WebDriver 和 JUnit 在 @AfterClass 注释方法中调用 driver.quit() 时出现 java.lang.NullPointerException

问题描述

我使用 JUnit 创建了一个测试,在 @AfterClass 类中,我使用 driver.quit () 命令在测试完成时关闭浏览器,但 eclipse 显示 java.lang.NullPointerException 消息。

测试类填充几个字段,然后在基中进行查询,在 Eclipse 控制台中显示结果,应该关闭浏览器但显示 java.lang.NullPointerException 消息。

下面是日志和测试脚本。

Eclipse 控制台

public class validarStatus {

private static WebDriver driver;

@Before
public void setUp() throws Exception {

    System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");

}

@Test
public void validarStatusOs() throws InterruptedException {

    WebDriver driver = new ChromeDriver();
    driver.get("http://10.5.9.45/BKOMais_S86825EstrategiaBackOfficeClaroFixo");
    driver.manage().window().maximize();

    // Logar BkoMais
    driver.findElement(By.id("matricula_I")).sendKeys("844502");
    driver.findElement(By.id("senha_I")).sendKeys("Pw34Jdt#*");
    driver.findElement(By.id("bt_entrar")).click();

    // Logar na Estratégia
    driver.findElement(By.id("mn_backoffice")).click();
    driver.findElement(By.id("mn_bkoffice_prod_203")).click();// Produto
    driver.findElement(By.id("mn_bkoffice_est_57")).click();// Estratégia

    // Selecionado a atividade
    Select atividade = new Select(driver.findElement(By.id("cboAtividade")));
    atividade.selectByIndex(3);

    // Registro >> Novo
    Thread.sleep(500);
    driver.findElement(By.id("mn_registro")).click();
    driver.findElement(By.id("mn_novo_caso")).click();

    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    // Cod Os Estratégia VREL
    String CodOs = driver.findElement(By.xpath("//*[@id=\"content\"]/div[1]/fieldset[1]/div[2]/div[3]/span"))
            .getText();

    // Campo Análise de Contrato
    Select analiseContrato = new Select(driver.findElement(By.id("cboMotivo")));
    analiseContrato.selectByIndex(5);

    try {
        // Campo Ação
        Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
        acao.selectByIndex(3);

        // Status
        WebDriverWait wait = new WebDriverWait(driver, 10);
        WebElement ele = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("cboStatus")));
        String valorStatus = ele.getText();
        // driver.findElement(By.id("cboStatus")).getText();
        Assert.assertEquals(" R", valorStatus);

        // Chamado
        driver.findElement(By.id("txtChamado")).sendKeys("Teste");

        // Observação
        driver.findElement(By.id("txtObservacao")).sendKeys("Teste 07/06/2018");

        // Botão Salvar
        driver.findElement(By.id("btnSalvar")).click();

    } catch (StaleElementReferenceException e) {

        // Campo Ação
        Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
        acao.selectByIndex(3);

        // Status
        WebDriverWait wait = new WebDriverWait(driver, 10);
        WebElement ele = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("cboStatus")));
        String valorStatus = ele.getText();

        // String valorStatus = driver.findElement(By.id("cboStatus")).getText();
        Assert.assertEquals(" R", valorStatus);
        // Chamado
        driver.findElement(By.id("txtChamado")).sendKeys("Teste");

        // Observação
        driver.findElement(By.id("txtObservacao")).sendKeys("Teste 07/06/2018");

        // Botão Salvar
        driver.findElement(By.id("btnSalvar")).click();

    } catch (Exception e) {

        // Campo Ação
        Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
        acao.selectByIndex(3);

        // Status
        String valorStatus = driver.findElement(By.id("cboStatus")).getText();
        Assert.assertEquals(" R", valorStatus);

        // Chamado
        driver.findElement(By.id("txtChamado")).sendKeys("Teste");

        // Observação
        driver.findElement(By.id("txtObservacao")).sendKeys("Teste 07/06/2018");

        // Botão Salvar
        driver.findElement(By.id("btnSalvar")).click();

    }

    // Select na base para validar o status da NU_OS
    ValidarEstrategiaPage p = new ValidarEstrategiaPage();
    p.returnNuOs(CodOs);

    // Saindo do Bko+
    Thread.sleep(1000);
    driver.findElement(By.linkText("Sair")).click();

}

@AfterClass
public static void closeBrowser() {

    driver.quit();

}}

标签: seleniumselenium-webdriverjunitwebdriverselenium-chromedriver

解决方案


您清楚地将 WebDriver 对象定义为方法内的局部变量:

@Test
public void validarStatusOs() throws InterruptedException {
  WebDriver driver = new ChromeDriver();

为了让 'After' 和 'Test' 方法都与全局变量交互,更改为:

@Test
public void validarStatusOs() throws InterruptedException {
  driver = new ChromeDriver();

顺便说一句,将您的班级名称从“validarStatus”更改为“ValidarStatus”。以大写字母开头的类名是 Java 的主要最佳实践。


推荐阅读