Unexpected "data:," windows open during selenium test - java

I am trying to test if the URL of the window is correct after clicking on a link. But unexpected windows with URL data:, get open between the test and getCurrentUrl grabs the "data:," as the URL and fails the test instead of the actual URL.
The windows with data:, is open even after all the test is complete.
Feature steps:
public void homePageOpens() {
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.titleContains("STORE"));
String homepageUrl = navigationUser.getUrl();
System.out.println(homepageUrl);
Assert.assertTrue(homepageUrl.contains("https://www.example.com/index.html"));
driver.close();
}
Navigation steps:
#Step("Get the URL")
public String getUrl() { return basePage.getUrl();
}
BasePage:
public String getUrl() {
System.out.println("just testing");
WebDriver driver = new ChromeDriver();
return driver.getCurrentUrl();
}

Replacing base page code with the following worked:
WebDriver driver = new ChromeDriver();
return driver.getCurrentUrl();
to
return getDriver().getCurrentUrl();

Related

Im trying to get the url of the page that Im in but I can't

public class WorkingWithChrome {
ChromeDriver driver;
String url = "http://qatechhub.com";
String urlface = "https://www.facebook.com/";
public void invokeBrowser() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\fabio\\eclipse-workspace\\libs\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
}
public void invokeBrowser2() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\fabio\\eclipse-workspace\\libs\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
driver.navigate().to(urlface);
driver.navigate().to(url);
driver.getCurrentUrl();
}
public static void main(String[] args) throws Exception {
WorkingWithChrome wc = new WorkingWithChrome();
wc.invokeBrowser();
wc.invokeBrowser2();
}
driver.quit();
}
}
Everythink is working, but the "getCurrentUrl()" when it get's to that aprt the code just stop's, the page dosnt even close.
I'v also tried to use String currentUrl = driver.getCurrentUrl(); but it didnt worked, does somebody know's how do I do it ?
(Im new in Selenium and in programming overall)
First you should add a delay between driver.navigate().to(url); and driver.getCurrentUrl(); to let the page loaded.
Now driver.getCurrentUrl(); returns a value as a String so you can print it as mentioned with System.out.println("Current URL : " + driver.getCurrentUrl());
And after that add driver.quit(); to quit
As #prophet has mentioned, your driver.quit(); is misplaced.
Also, driver.getCurrentUrl(); returns the string. So unless you accept that value in some variable and print it out, you won't know what it's doing.
So please do something like following :
public void invokeBrowser2() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\fabio\\eclipse-workspace\\libs\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
driver.navigate().to(urlface);
driver.navigate().to(url);
System.out.println("Current URL : " + driver.getCurrentUrl());
driver.quit();
}

Having trouble with SkipException, testNG, what am I doing wrong?

I am very new to selenium UI automation and I am trying my hands on with a simple application. I am using java with testNG. I need to integrate this test with CI and the test environment url will be different with every deployment. I am passing this as a parameter, difference between test and prod environment is that in the test, there won't be any login screen hence there is no need for authentication but my test verifies login and a login method. I need to be able to skip this test based on the URL supplied. Here is my code and the problem is testNG always suggests that the test was skipped but I can see it executing the login method. Please help me correct or understand what mistake I am committing.
public class Login {
WebDriver driver;
//Parameter - test environment URL
String url = System.getProperty("environment");
#Test (priority = 1)
public void verifyLogin() {
//Skip verifyLogin test in non prod environments
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
if (url=="www.google.com")
//If url matches production url then execute the test, if url doesn't match production URL then skip login test as there won't be any authentication/login in non prod
{
LoginPage login = new LoginPage(driver);
login.signIn();
Assert.assertEquals(driver.getTitle(), "Application Name", "Login failed,");
String title = driver.getTitle();
System.out.println("Page title is " + title);
driver.close();
}
else if (url!="www.google.com"){
throw new SkipException("Skipping this test");
}
}
#Test(priority = 2)
public void userKey() {
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
//If URL equals prod then call the login method to be able to login to the app else just execute the userKey method without having the need to login
if (url=="www.google.com");
{
LoginPage login = new LoginPage(driver);
login.signIn();
}
AccountManagementPage userKey = new AccountManagementPage(driver);
userKey.key();
driver.close();
}
}
The very exact use case is well explained here without throwing SkipException.
The idea is to create a custom annotation for the methods to be skipped & read the methods using IMethodInterceptor - decide to execute or not behind the scenes.
Updated for the question in the comment section:
You do not have to worry about 'TestEnvironment' or 'TestParameters' class.Just directly use the production check logic here.
Predicate<IMethodInstance> isTestExecutingOnProduction = (tip) -> {
return system.getProperty("env").
.equals("<production check here>");
};

driver.getcurrenturl () does not take the correct url

I'm trying to learn selenium webdriver so I started with the basics but my driver.getcurrenturl() does not take the correct url.
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://test.com/");
driver.findElement(By.name("SelectedDomainName")).sendKeys("a");
driver.findElement(By.id("UserName")).sendKeys("b");
driver.findElement(By.id("Password")).sendKeys("#123");
driver.findElement(By.id("loginBtn")).click();
String Url = driver.getCurrentUrl();
if (Url.equals("http://test.com/Home/Index")) {
System.out.println("Login successful");
} else {
System.out.println("Login Failed");
}
}
String url = driver.getCurrentUrl();
This will take the current url, but you need to go to some url first so you need to mention:
driver.get("https://www.test.com/index.html");
After that you can put it in string and validate.
WebDriver driver = new FirefoxDriver();
driver.get("https://www.test.com/index.html");
String url = driver.getCurrentUrl();
if(url.equals("https://www.test.com/index.html")) {
System.out.println("Login successful");
} else {
System.out.println("Incorrect details provided by the User");
}
What URL are you expecting to get? Make sure you navigate to that URL first (see below) and that the navigation has been complete.
driver.get("http://www.google.com");
Before you compare it to other string, print it out and see what's there. Just note that if you are trying to obtain the URL before the navigation has taken place, it will return about:blank string to you.

webdriver on safari works well in debug mode, but could not work well when I run it

I test safari browser on Windows with Selenium Webdriver, when in debug mode, my script works well, but when I run it, it could not work well. Does anyone encounter this situation?
public class JustForTestSafari {
public WebDriver driver;
#Test
public void f() {
driver = new SafariDriver();
String baseURL = "http://universitytest.emersonprocess.com/";
String expectedTitle = "ProcessWorld - Your Connection to Global Information";
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get(baseURL);
driver.manage().window().maximize();
String actulTitle = driver.getTitle();
Assert.assertEquals(expectedTitle, actulTitle);
driver.findElement(By.id("_ctl0_mastercontent_username")).clear();
driver.findElement(By.id("_ctl0_mastercontent_username")).sendKeys("***.guo");
driver.findElement(By.id("_ctl0_mastercontent_password")).clear();
driver.findElement(By.id("_ctl0_mastercontent_password")).sendKeys("*******");
driver.findElement(By.id("_ctl0_mastercontent_btn_standardsignin")).click();
}

Web driver is not performing actions on web page

I'm automating a php web page using webdriver and java language. My code was executing and I was able to perform actions on web page, but today only the login method is executing and my second method gets failed everytime I run. I'm so worried why it is happening. Please help, I'm new in automation testing.
public class TestNGClass {
private String baseUrl = "http://test.com/test2/1.4.6.3/public/admin/";
private WebDriver driver = new FirefoxDriver();
#Test
public void login() {
driver.manage().window().maximize();
driver.get(baseUrl);
driver.findElement(By.name("username")).sendKeys("abc");
driver.findElement(By.name("password")).sendKeys("123");
driver.findElement(By.name("login")).submit();
System.out.print("\nCongrats..You have successfully logged in.");
}
#Test
public void createUser() {
String expectedTitle = "User";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle,"Title Not Found!");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//body/div[3]/div[2]/ul/li[2]/a/img")).click();
Error :
java.lang.AssertionError: Title Not Found! expected [User] but found []
This is because you are using Assert.
Assert.assertEquals(actualTitle, expectedTitle,"Title Not Found!");
Make use of Try catch in-order to proceed next step.
try{
Assert.assertEquals(actualTitle, expectedTitle,"Title Not Found!");
}catch (Exception e)
{
}

Categories