0

I want to get url(baseUrl = "http://google.com";) that is declared in selenium test class from my java class.how can i get the url

is there any selenium api class. Please suggest me

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();
  }
3
  • do you want to print the current URL? Commented Feb 28, 2014 at 10:11
  • yes.but, in some other class i want to get or print the url Commented Feb 28, 2014 at 10:13
  • some other class in the sense? Commented Feb 28, 2014 at 10:15

2 Answers 2

4

The driver.getCurrentUrl() method will give you what you want. It'll return the full URL (like this), so you'll need to chop the string off after the .com.

Sign up to request clarification or add additional context in comments.

6 Comments

Above i added my selenium test class code.In that i declared a url(baseUrl = "localhost:8080/";).i want to get that url to my new java class.How can i get the URL.If i use driver.getCurrentUrl in my class i am just getting empty(no url)
I'm not sure if I understand the question. Do you mean that you have a separate class to SeleJunit (called OtherClass, for the sake of example), and you want to get the current URL in a method of OtherClass?
seperate class but not the test class. this seperate class will take the selenium test class as input and need to display the url for which the test class has been written.
Okay, gotcha. Add an accessor method for baseUrl to your SeleJunit class. Then in OtherClass, you can write something like testUrl = test.getBaseUrl(). You could also make baseUrl public and write testUrl = test.baseUrl instead.
I should not make any changes on test class.My goal is to find the missing selenium test case based on url. first i got the list of urls for a single main url(like google.com) using jsoup. Now i need to get the urls from the selenium test programatically and compare these urls to check whether testcase for that particular url exist or not.
|
0

JAVA

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


PYTHON

print driver.current_url

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.