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>");
};
Related
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();
I use selenium WebDriver. I am trying to run following scenario.
I launch a url, and I want to add a condition that if there is any url opened other than the one I intended, I want to close it.
Following is my code, I will explain whats happening with it below.
if (config.getProperty("browser").equals("Chrome"))
{
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
selectServer();
String currentURL = driver.getCurrentUrl();
if (currentURL != config.getProperty("production") || currentURL != config.getProperty("staging") || currentURL != config.getProperty("development"))
{
Thread.Sleep(10000); //for Debugging purpose
driver.close();
}
}
I have a config.properties file where I set the browser / server selection.
Now what happens is, when I launch the test, chrome launches and:
Chrome settings window opens and it asks me to restore default settings. (Window in display)
My intended URL opens up. (hidden)
When I run the test, the test passes but Chrome Settings window does not close. I tried to print the current URL , and it returns the production server URL which is my intended URL but the browser window in display is not my production URL.
Based on your response in comments, I believe you need to switch to the settings tab, close that tab, then switch back to the original tab. The below code should do that:
public static void closeBrowserTab() {
String originalHandle = driver.getWindowHandle();
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(originalHandle)) {
driver.switchTo().window(handle);
driver.close();
break;
}
}
driver.switchTo().window(originalHandle);
}
I am working on frame based website. I am using selenium 2.47.1 & PhantomJS 1.9.2. I have written Automation Script & run it using firefox driver, it works perfectly. I am trying to execute the code with help PhanthomJS driver. But whenever I am trying to execute the it gives NoSuchFrameFoundException. My script is given below...
public class iFrame {
public String baseUrl = "https://test5.icoreemr.com/interface/login/login.php";
public WebDriver cd;
String scenarioName = "Sheet1";
ExcelLibrary ex = new ExcelLibrary();
#BeforeTest
public void SetBaseUrl() {
Capabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"C:\\Users\\Probe7\\Downloads\\phantomjs-1.9.2-windows\\phantomjs.exe"
);
this.cd = new PhantomJSDriver(caps);
cd.get(baseUrl);
cd.manage().window().maximize();
}
#Test(priority = 0)
/* Login into the Application */
public void Login() {
cd.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
cd.switchTo().frame("Login");
System.out.println("Control Moved to Login Frame");
String UserName = ex.getExcelValue(scenarioName, 2, 4);
cd.findElement(By.xpath("//body/center/form/table/tbody/tr/td/div/div[2]/table/tbody/tr[1]/td[2]/input")).sendKeys(UserName);
}
Above code gives following exception..
org.openqa.selenium.NoSuchFrameException: No frame element found by name or id Login
Eventhough same script works perfectly in firefox driver, but not in PhantomJS.
Please advice me what went wrong?? I am looking into this nearly a day, but didn't find any solution.. Please help me on this?
Thanks in Advance...
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)
{
}
I'm trying to access a webpage using Selenium.
I use the WebDriver and HtmlUnitDriver classes:
WebDriver driver = new HtmlUnitDriver();
WebElement element;
Then to get a webpage I use:
driver.get("url");
url being the specific url of a page.
However, this does not work for all pages? When accessing some pages the program just halts and nothing happens. What I'm doing is logging into a web page, buying some stuff, and all that works, but when I want to go to the check-out and finish the order the check-out page does not load.
you can use some functions like these
public String openBrowser(String object, String data,String configPath) {
APP_LOGS.debug("Opening browser");
if (CONFIG.getProperty(data).equals("Mozilla"))
driver =new FirefoxDriver();
else if (CONFIG.getProperty(data).equals("IE"))
driver =new InternetExplorerDriver();
else if (CONFIG.getProperty(data).equals("Chrome"))
driver =new ChromeDriver();
else if (CONFIG.getProperty(data).equals("Safari"))
driver =new SafariDriver();
driver.manage().window().maximize() ;
}
public String clickButton(String object, String data,String configPath){
OR=new ObjectRepLocator(ObjectRepo);
APP_LOGS.debug("Clicking on Button");
try{
driver.findElement(By.xpath(OR.getLocator(object,configPath))).click();
}catch (Exception e){
return Constants.KEYWORD_FAIL+ " Not able to click on button"+e.getMessage();
}
return Constants.KEYWORD_PASS;
}
Better way is you can get those values for processing this from Excel sheets. I am using the data driven framework that proceeds in this way