如何从java中的selenium测试类获取url

时间:2014-02-28 10:05:13

标签: java selenium

我想从我的java class.how获取在selenium测试类中声明的url(baseUrl = "http://google.com";)我可以获取url

是否有任何selenium api类。请建议我

public class SeleJunit {
  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://localhost:8080/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testSeleJunit1() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.linkText("New Item")).click();
    driver.findElement(By.id("name")).click();
    driver.findElement(By.id("name")).clear();
    driver.findElement(By.id("name")).sendKeys("i18n2");
    driver.findElement(By.name("mode")).click();
    driver.findElement(By.id("ok-button")).click();
    new Select(driver.findElement(By.name("_.compressionLevel"))).selectByVisibleText("System Default");
    // ERROR: Caught exception [Error: Dom locators are not implemented yet!]
    driver.findElement(By.id("side-panel")).click();
    driver.findElement(By.id("yui-gen30-button")).click();
    driver.findElement(By.linkText("Jenkins")).click();
  }

2 个答案:

答案 0 :(得分:4)

driver.getCurrentUrl()方法会为您提供所需内容。它会返回完整的网址(例如this),因此您需要在.com后关闭字符串。

答案 1 :(得分:0)

JAVA

  

的System.out.println(driver.getCurrentUrl());


PYTHON

  

print driver.current_url