close() is not working, everytime a new window gets open - java

I am trying run the below mentioned code, its not working , everytime a new window gets open instead of closing the current window and opening a window.
public class Next2beforendafter {
public static WebDriver driver;
#BeforeMethod
public void launchBrowser() {
System.setProperty("webdriver.gecko.driver", "C:/Users/xyz/Videos/selenium/geckodriver-v0.11.1-win64/geckodriver.exe");
driver = new FirefoxDriver();
}
#Test(priority = 1)
public void verifygoogleTitle() {
driver.get("http://www.google.com");
Assert.assertEquals("Google", driver.getTitle());
}
#Test(priority = 2)
public void verifyyahooTitle() {
driver.get("https://in.yahoo.com");
Assert.assertEquals("Yahoo", driver.getTitle());
}
#Test(priority = 3)
public void verifybankofindiaTitle() {
driver.get("http://www.bankofindia.co.in/english/home.aspx");
Assert.assertEquals("Bank Of India - Home", driver.getTitle());
}
#AfterMethod
public void closeBrowser() {
driver.close();
}
}
Selenium Ver.3.0.1

You should be using driver.quit() if you want to close the webdriver. driver.close() only closes the current window (tab) but leaves the driver session open

Related

chrome browser doesn't closes after finishing test

#BeforeMethod
public void setup ()
{
System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\chromedriver.exe");
driver = new ChromeDriver(); // Opens Chrome browser
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
//driver.manage().window().maximize();
}
#AfterMethod
public void tearDown() throws Exception
{
Thread.sleep(5*1000);
driver.quit();
}
#Test
public void browserCommandsTests()
{
try {
driver.get("http://www.costco.com");
Thread.sleep(3 * 1000);
driver.navigate().to("http://www.facebook.com");
Thread.sleep(3 * 1000);
driver.navigate().back();
Thread.sleep(3 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
When i'm testing this code after opening the urls it doesn't closes the
testing browser.
it opens only 1 link and closes browser when i'm getting only 1 url. but as soon as i try to get 2nd url it opens 2nd url and after it doesn't closes the browser under test.
It is your tag #AfterMethod change it to AfterClass (also you can try close instead of quit):
#AfterClass
public void tearDown() throws Exception
{
driver.close();
}
Also, friendly advice, using the thread.sleep will cause you more grief than joy. The alternative is to use driver.findElement() in a try/catch and depending on the result (true/false) direct your flow from there.
Best of luck!
#AfterMethod
public void tearDown() throws Exception
{
driver.quit();
}

Element is not currently visible and so may not be interacted with Command duration or timeout: 28 milliseconds

i have tried to execute multi logins(using TestNG), my first test1 is passed but remaining tests gives some error like "Element is not currently visible and so may not be interacted with Command duration or timeout: 28 milliseconds". Kindly resolve my issue
String baseUrl = "******************";
WebDriver driver= new FirefoxDriver();
#BeforeTest
public void b_url()
{
driver.get(baseUrl);
}
#Test(priority=1)
public void login1() throws InterruptedException
{
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.findElement(By.xpath("//*[#id='w1']/li/a")).click();
driver.findElement(By.id("LoginFormusername")).sendKeys("Boopathi");
driver.findElement(By.id("LoginForm-password")).sendKeys("test");
driver.findElement(By.xpath("//*[#id='login-form']/div/div/div/div/div[3]/input")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//*[#id='w6']/li/a")).click();
driver.findElement(By.xpath("//*[#id='w7']/li[3]/a")).click();
//driver.navigate().refresh();
}
#Test(priority=2)
public void login2() throws InterruptedException
{
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.findElement(By.xpath("//*[#id='w1']/li/a")).click();
driver.findElement(By.id("LoginForm-username")).sendKeys("john");
driver.findElement(By.id("LoginForm-password")).sendKeys("test");
driver.findElement(By.xpath("//*[#id='login-form']/div/div/div/div/div[3]/input")).click();
Thread.sleep(6000);
driver.findElement(By.xpath("//*[#id='w6']/li/a")).click();
driver.findElement(By.xpath("//*[#id='w7']/li[3]/a")).click();
driver.navigate().refresh();
}
#Test(priority=3)
public void login3() throws InterruptedException
{
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.findElement(By.xpath("//*[#id='w1']/li/a")).click();
driver.findElement(By.id("LoginForm-username")).sendKeys("Kamal");
driver.findElement(By.id("LoginForm-password")).sendKeys("test");
driver.findElement(By.xpath("//*[#id='loginform']/div/div/div/div/div[3]/input")).click();
driver.navigate().refresh();
Thread.sleep(6000);
driver.findElement(By.xpath("//*[#id='w6']/li/a")).click();
driver.findElement(By.xpath("//*[#id='w7']/li[3]/a")).click();
}
#AfterTest()
public void logout()
{
driver.quit();
}
Navigate application each time
#BeforeMethod
public void b_url()
{
driver.get(baseUrl);
}
Add refresh code in each test after deleting cookies
driver.navigate().refresh();
Let me know this will not work

Able to locate frame with selenium, but doesn't work with Junit Cucumber

I am able to locate a frame using a selenium java program. The following code runs successfully:
public static void TestCase() throws IOException, InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("website");
driver.findElement(By.xpath("//*[#id='username']")).sendKeys(userName);
driver.findElement(By.xpath("//*[#id='password']")).sendKeys("password");
driver.findElement(By.xpath("//*[#id='session_link']")).click();
driver.findElement(By.xpath("//*[#id='app_236']/a")).click(); //Click a link on the page
driver.switchTo().frame("iframe100-100"); //Switch to a frame within the new page
driver.findElement(By.xpath("//*[#id='yui-gen0']/li[2]/div")).click(); //Click element within the frame
}
But if I run the same code using a Cucumber Junit maven project, the program is not able to locate the frame.
#Given("^I log into website$")
public void log_in()
{
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("website");
driver.findElement(By.xpath("//*[#id='username']")).sendKeys(userName);
driver.findElement(By.xpath("//*[#id='password']")).sendKeys("password");
driver.findElement(By.xpath("//*[#id='session_link']")).click();
}
#When("^I select the link from homepage$")
public void select_link() throws InterruptedException
{
driver.findElement(By.xpath("//*[#id='app_236']/a")).click();
}
#Then("^I should see something$")
public void click_element_within_frame()
{
driver.switchTo().frame("iframe100-100"); //Switch to a frame within the new page
driver.findElement(By.xpath("//*[#id='yui-gen0']/li[2]/div")).click(); //Click element within the frame
}
Error message :
31morg.openqa.selenium.NoSuchFrameException: Unable to locate frame: iframe100-100
Any suggestions ?

Unable to identify the login button for naukri.com application using selenium Webdriver

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

Selenium WebDriver using TestNG and Excel

Hello i really need help with Selenium WebDriver using TestNG and Excel
i try to get data from excel to open browser and navigate URL. its work successfully and terminal and testng report showing test pass but its not open browser or doing anything its just run its self and show report
Config File
public void openBrowser(String browser){
try {
if (browser.equals("Mozilla")) {
driver = new FirefoxDriver();
} else if(browser.equals("IE")){
driver = new InternetExplorerDriver();
} else if(browser.equals("Chrome")){
System.setProperty("webdriver.chrome.driver", "\\Applications\\Google Chrome.app\\Contents\\MacOS\\Google Chrome ");
driver = new ChromeDriver();
}
} catch (Exception e) {
}
}
public void navigate(String baseUrl){
try {
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.navigate().to(baseUrl);
} catch (Exception e) {
}
}
And Test Execute File
public class NewTest {
public String exPath = Config.filePath;
public String exName = Config.fileName;
public String exWrSheet = "Logiin Functional Test";
public Config config;
#BeforeTest
public void openBrowser() {
config = new Config();
Excel.setExcelFile(exPath+exName, exWrSheet);
String browser = Excel.getCellData(1, 2);
config.openBrowser(browser);
}
#BeforeMethod
public void navigate() {
config = new Config();
Excel.setExcelFile(exPath+exName, exWrSheet);
String baseUrl = Excel.getCellData(2, 2);
config.navigate(baseUrl);
}
#Test
public void test() {
System.out.println("Test");
}
#AfterTest
public void closeBroser() {
//Config.tearDown();
}
I don't have quite enough rep to make a comment, which I would prefer here, but if you aren't getting any sort of exception, I'd suspect the value you're getting for the browser variable is not matching anything in your if-then-else in openBrowser and then falling through. Step through the code with a debugger or just add:
String browser = Excel.getCellData(1, 2);
System.out.println("Browser value from Excel =" + browser);
config.openBrowser(browser);
to see what you're reading from the file.
1 - TestNg is always pass because you are using "void" method and you catch "all" exception
2 - No browser opened because in openBrowser(String browser), NullPointException throws and you already catch it.
-> you need to init an instance of WebDriver and pass it through your test.

Categories