TestNG Selenium WebElement cannot find - java

Below is my code running script through TestNG. It fails to find the web element username and the error I get (screenshot attached). Please advise.
#Test
System.setProperty("webdriver.chrome.driver", "C:/Selenium/chromedriver.exe");
WebDriver Driver = new ChromeDriver();
String baseURL = "http://mapp.com";
Driver.get(baseURL);
String UserName = "*****";
String Password = "*****";
WebElement username = Driver.findElement(By.cssSelector("input#Login"));

Related

Unexpected "data:," windows open during selenium test

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();

Selenium Web Driver script for login page results in an error

My code:
package pak0310;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class class0310
{
public static void main(String[] args)
{
// objects and variables instantiation
WebDriver driver = new FirefoxDriver();
String appUrl = "https://accounts.google.com";
// launch the firefox browser and open the application url
driver.get(appUrl);
// maximize the browser window
driver.manage().window().maximize();
// declare and initialize the variable to store the expected title of the webpage.
String expectedTitle = " Sign in - Google Accounts ";
// fetch the title of the web page and save it into a string variable
String actualTitle = driver.getTitle();
// compare the expected title of the page with the actual title of the page and print the result
if (expectedTitle.equals(actualTitle))
{
System.out.println("Verification Successful - The correct title is displayed on the web page.");
}
else
{
System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
}
// enter a valid username in the email textbox
WebElement username = driver.findElement(By.id("Email"));
username.clear();
username.sendKeys("TestSelenium");
// enter a valid password in the password textbox
WebElement password = driver.findElement(By.id("Passwd"));
password.clear();
password.sendKeys("password123");
WebElement SignInButton = driver.findElement(By.id("signIn"));
SignInButton.click();
// close the web browser
driver.close();
System.out.println("Test script executed successfully.");
// terminate the program
System.exit(0);
}
}
Error:
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:754)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:115)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:329)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:103)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:99)
at pak0310.class0310.main(class0310.java:22)
...
In this similar question (https://stackoverflow.com/a/38752315/8195985) user Paras answered:
You are using latest version of Selenium WebDriver i.e. Selenium 3.x, this version of webdriver doesn't support direct firefox launch. You have to set the SystemProperty for webdriver.gecko.driver.
Replace the Code:-
WebDriver driver; =new FirefoxDriver();
With This code:-
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "<Path to your WebDriver>");
driver =new FirefoxDriver();
Maybe use that will help you.
Download geckodriver from https://github.com/mozilla/geckodriver/releases
unzip folder you will get like this geckodriver.exe
Copy that paste in some where and copy the complete path with driver.
use below code in <path> place paste your path
System.setProperty("webdriver.gecko.driver", "your path");
WebDriver driver = new FirefoxDriver();
This error suggesting to use of geckodriver (post Selenium 3), to launch Firefox. You can refer
How to use the gecko executable with Selenium

IE11 exceptions with IEDriverServer: Unable to get browser

Yesterday Internet Explorer got updated (11.0.44 update), then below code prompts me with the following error not able to get browser.
I have set HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE key of iexplore.exe with 0.
Anyone can help me out?
Code:
String IEDriver_64 = "D:/Tools/IEDriverServer.exe";
System.setProperty("webdriver.ie.driver", IEDriver_64);
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
WebDriver driver = new InternetExplorerDriver(capabilities);
//capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
String baseUrl = "https://xxx//member/login.php";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(baseUrl);
Thread.sleep(1000*30);
System.out.print("driver.getCurrentUrl()1="+driver.getCurrentUrl());
System.out.print("driver.getTitle()1="+driver.getTitle());
System.out.print("-------------1-------------");
String currenthandle = driver.getWindowHandle();
System.out.print("-------------2-------------");
System.out.print("currenthandle="+currenthandle);
Thread.sleep(1000*5);
driver.findElement(By.id("SUBMIT_LOGIN")).click();

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();
}

org.testng.TestNGException: Cannot instantiate class TestNG.DemoTestNG

Here is my problem.
After executing testNG script , I'm getting error stating
org.testng.TestNGException:
Cannot instantiate class TestNG.DemoTestNG. Can anybody please give me the solution for my problem.
public class DemoTestNG {
WebDriver driver = new FirefoxDriver();
String appUrl = "http://192.168.1.116:9090/soxaudit/login";
#Test
public void Sox_Login() {
driver.get(appUrl);
driver.manage().window().maximize();
String expectedTitle = "SOX ADMIN - LOGIN";
String actualTitle = driver.getTitle();
Assert.assertEquals(expectedTitle,actualTitle);
WebElement username = driver.findElement(By.id("j_username"));
username.clear();
username.sendKeys(new String[]{"sox"});
WebElement password = driver.findElement(By.id("j_password"));
password.clear();
password.sendKeys(new String[]{"welcome#123"});
WebElement SignInButton = driver.findElement(By.xpath("//*[#id='login-page']/div/form/div/button"));
SignInButton.click();
// close the web browser
driver.close();
System.out.println("Test script executed successfully.");
// terminate the program
System.exit(0);
}
}

Categories