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);
}
}
Related
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"));
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();
}
I am trying to automate the naukri.com application and as a part of that when I launch the site it basically displays some set of pop up windows which needs to be closed before I proceed to login to the application.This specific functionality has been handled by the code where it closes all the pop up windows and when I proceed to click on the login button, the login button link is not identified and script fails.If i comment the pop up window code then the login button is identified.Please find the below code for the same and kindly help me to resolve the issue
public class naukri {
WebDriver driver = new FirefoxDriver();
#Test
public void pagelaunch() throws InterruptedException{
driver.get("http://www.naukri.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String parenthandle = driver.getWindowHandle();
String parent = driver.getWindowHandle();
//close all the pop ups
Set<String> pops=driver.getWindowHandles();
Iterator<String> it =pops.iterator();
while (it.hasNext()){
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
}
}
System.out.println("the system handle is"+parenthandle);
//to click on login button and proceed to login to the application
driver.findElement(By.xpath("//a[#title='Jobseeker Login']")).click();
Thread.sleep(5000);
for(String winhandle:driver.getWindowHandles())
{
driver.switchTo().window(winhandle);
}
driver.findElement(By.xpath("//a[#id='uSel']")).click();
driver.findElement(By.xpath("html/body/div[8]/div[2]/div[2]/form/div[4]/div[2]/input")).sendKeys("anand_qa");
driver.findElement(By.xpath("html/body/div[8]/div[2]/div[2]/form/div[5]/div[2]/input")).sendKeys("test1234");
driver.findElement(By.xpath("//div[8]/div[2]/div[2]/form/div[7]/button")).click();
driver.switchTo().window(parenthandle);
}
}
public class naukri {
WebDriver driver = new FirefoxDriver();
#Test
public void pageLaunch() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.naukri.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String parenthandle = driver.getWindowHandle();
String parent = driver.getWindowHandle();
//close all the pop ups
Set<String> pops=driver.getWindowHandles();
{
Iterator<String> it =pops.iterator();
while (it.hasNext()) {
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
}
}
}
System.out.println("the system handle is"+ driver.switchTo().window(parenthandle).getTitle());
//to click on login button and proceed to login to the application
driver.findElement(By.xpath("//a[#id='login_Layer']")).click();
Thread.sleep(5000);
/*for (String winhandle:driver.getWindowHandles())
{
driver.switchTo().window(winhandle);
}*/
driver.findElement(By.xpath("//a[#id='uSel']")).click();
driver.findElement(By.xpath("//form[#id ='lgnFrm']/div[4]/div[2]/input[#id='uLogin']")).sendKeys("anand_qa");
driver.findElement(By.xpath("//form[#id ='lgnFrm']/div[5]/div[2]/input[#id='pLogin']")).sendKeys("test1234");
driver.findElement(By.xpath("//form[#id='lgnFrm']/div[7]/button")).click();
}
}
#AK17:
Your code had 2 problems
1.You were not switching to parenthandle after closing the pop-up windows, I added the code
driver.switchTo().window(parenthandle);
2.Your locators for username,password and login button were not correct
Working code, try this:
public class naukri {
WebDriver driver = new FirefoxDriver();
#Test
public void pagelaunch() throws InterruptedException{
driver.get("http://www.naukri.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String parenthandle = driver.getWindowHandle();
String parent = driver.getWindowHandle();
//close all the pop ups
Set<String> pops=driver.getWindowHandles();
Iterator<String> it =pops.iterator();
while (it.hasNext()){
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
}
}
System.out.println("the system handle is"+parenthandle);
driver.switchTo().window(parenthandle);
WebDriverWait wait = new WebDriverWait(driver,10);
WebElement login = driver.findElement(By.xpath("//a[#title='Jobseeker Login']"));
wait.until(ExpectedConditions.elementToBeClickable(login));
//to click on login button and proceed to login to the application
driver.findElement(By.xpath("//a[#title='Jobseeker Login']")).click();
Thread.sleep(3000);
for(String winhandle:driver.getWindowHandles())
{
System.out.println("login: "+winhandle);
driver.switchTo().window(winhandle);
}
Thread.sleep(3000);
driver.findElement(By.xpath("//a[#id='uSel']")).click();
driver.findElement(By.xpath(".//*[#id='uLogin']")).sendKeys("anand_qa");
driver.findElement(By.xpath(".//*[#id='pLogin']")).sendKeys("test1234");
driver.findElement(By.xpath(".//*[#id='lgnFrm']/div[7]/button")).click();
//driver.switchTo().window(parenthandle);
}
}
I am not having any luck getting Phantomjs to access a url in my Selenium webdriver tests. I continue to get the following error after the assert -
Expected :Login
Actual :403 - Forbidden: Access is denied.
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at
Here is my - Also HTML Unit Driver does the same thing. However when I use Firefox driver it runs fine.
public class EasyElectPhantomTest {
private WebDriver driver;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
private String baseUrl;
#Before
public void setUp() throws Exception {
driver = new PhantomJSDriver();
baseUrl = "https://secure02.pilot.principal.com/";
#Test
public void testEasyElectPhantom() throws Exception {
driver.get(baseUrl + "/login?token=ODEFIJMLEHGIOPRNGIYN");
driver.manage().window().maximize(); //Maximizing window
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
assertEquals("Login", driver.getTitle());
driver.findElement(By.id("firstName")).sendKeys("Ryan");
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)
{
}