使用selenium和firefox浏览器进行自动测试

时间:2014-03-22 20:57:14

标签: java selenium junit

我下载了下面的代码并将其用于某些测试,而且它是昨天运行的,但从今天开始代码停止工作。我的测试现在失败了以前没有发生过。它抛出一个错误,说org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此无法与元素交互。

package org.openqa.selenium.example;

//import org.openqa.selenium.browserlaunchers.locators.GoogleChromeLocator;

//import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.junit.*;

import static org.junit.Assert.*;

//import org.openqa.selenium.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.chrome.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.Select;
//import org.openqa.selenium.net.UrlChecker;

import static org.junit.Assert.assertEquals;


import org.junit.Test;

public class KongaUrlTest 
{

      private WebDriver driver;
      private String baseUrl;
      //private boolean acceptNextAlert = true;
      private StringBuffer verificationErrors = new StringBuffer();

      @Before
      public void setUp() throws Exception 
      {
        driver = new FirefoxDriver();
        baseUrl = "http://www.konga.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
      }

      @Test
      public void testFirefoxWebdriver() throws Exception 
      {
        driver.get(baseUrl);
        driver.findElement(By.cssSelector("a.vertnavlink > span")).click();
        try 
        {
          assertEquals("Phones & Tablets | Konga Nigeria", driver.getTitle());
          System.out.println(driver.getTitle());
        } 
        catch (Error e) 
        {
          verificationErrors.append(e.toString());
        }
      }

      @After
      public void tearDown() throws Exception 
      {
        System.out.println(driver.getCurrentUrl());
        driver.quit();

      }



}

1 个答案:

答案 0 :(得分:0)

正在显示阻止对话框。每次Selenium打开新浏览器并导航到该站点时,可能会显示它。首先关闭该对话框:

driver.get(baseUrl);
try
{
    driver.findElement(By.cssSelector(".fancybox-close")).click();
}
catch { }
driver.findElement(By.cssSelector("a.vertnavlink > span")).click();