My scenario is, when I hit the URL the page should navigate to the authentication window and after enter the valid user ID and password, the main page gets displayed and upon clicking any link in the main page, a new window will open and displays the corresponding page without any authentication.
Issue : when I use the switchTo() in the code, clicking the link from the main page is opening a new window and again prompting me to enter the user ID and pass.
If I remove that switchTo(), upon clicking it is taking me to the expected page wihout authentication.
**Code** import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.junit.*;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class MKTEvent {
public WebDriver driver;
public String baseUrl;
public void testMKTEvent() throws IOException, BiffException, InterruptedException {
driver = new InternetExplorerDriver();
baseUrl = "<<URL>>";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl);
File f=new File("D:\\User ID.xls");
Workbook w=Workbook.getWorkbook(f);
Sheet s=w.getSheet(0);
for(int i=1;i<s.getRows();i++)
{
String uname=s.getCell(0,i).getContents();
String pass=s.getCell(1,i).getContents();
driver.findElement(By.name("j_username")).clear();
driver.findElement(By.name("j_username")).sendKeys(uname);
driver.findElement(By.name("j_password")).clear();
driver.findElement(By.name("j_password")).sendKeys(pass);
driver.findElement(By.id("submit")).click();
Thread.sleep(15000);
driver.findElement(By.id("Dashboard")).click();
String parentWindow = driver.getWindowHandle();
System.out.println(parentWindow);
for(String windowHandle : driver.getWindowHandles()){
if(!windowHandle.equals(parentWindow))
{
driver.switchTo().window(windowHandle);
String nt = driver.findElement(By.id("createRequest")).getText();
System.out.println(nt);
driver.findElement(By.id("createRequest")).click();
}
}
}
}
}
Please help me to fix this issue.
Using DesiredCapabilities will solve your issue.
DesiredCapabilities IECapabilities = DesiredCapabilities.internetExplorer();
IECapabilities.setCapability("enablePersistentHover", false);
IECapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
IECapabilities.setCapability("ignoreProtectedModeSettings", true);
IECapabilities.setCapability("ie.ensureCleanSession", false);
Related
I am unable to type text in login page, tried all the solutions provided online but issue still exists. anyone help me to resolve the issue. below is the code:
package com.individual.newlearn;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class secondProgram {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in/webhp?ie=UTF-8&rct=j&gws_rd=cr&dcr=0&ei=9Wq2WozGFIH4vgSJ2LmoAw");
Thread.sleep(2000);
driver.findElement(By.xpath("//a[text()='Gmail']")).click();
//driver.navigate().to("https://accounts.google.com/signin/v2/identifier?hl=EN&flowName=GlifWebSignIn&flowEntry=ServiceLogin");
driver.findElement(By.xpath("//a[text()='Sign In']")).click();
Thread.sleep(2000);
driver.findElement(By.id("identifierId")).sendKeys("mail id");
java script i used for typing mail id:
/*WebElement searchbox = driver.findElement(By.id("identifierId"));
JavascriptExecutor myExecutor = ((JavascriptExecutor)driver);
myExecutor.executeScript("arguments[0].value='mail id';", searchbox);
Thread.sleep(3000);*/
I'm trying to right click a link in a web page and open it in a new window with context click method of Action class. Below is the code which I got from few references. But this doesn't work. Can someone help to find out what is the mistake I'm doing here?
package webDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class RightClick {
public static void main(String args[]) throws Exception
{
String baseUrl = "https://www.google.com";
System.setProperty("WebDriver.gecko.driver","C://geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get(baseUrl);
WebElement select = driver.findElement(By.linkText("About"));
Actions builder = new Actions(driver);
builder.contextClick(select).perform();
builder.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER);
}
}
Is there any other way to do this?
Thanks in Advance
Try the below code your problem will be solved,Its working I have tried it for you,Kindly revert back if you have got it.
public class Demo {
public static void main(String[] args) throws AWTException {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"/Drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in/?gfe_rd=cr&dcr=0&ei=sxWqWoDHL6SwX7PXjaAH");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
WebElement element=driver.findElement(By.xpath(".//*[#id='fsl']/a[3]"));
String linkToOpen= element.getAttribute("href");
System.out.println(linkToOpen);
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_T);
//To switch to the new tab
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
//To navigate to new link/URL in 2nd new tab
driver.get(linkToOpen);
}
}
When we click a link while pressing Shift key, the resulting link generally opens in new window.
See if this helps:
WebElement link = driver.findElement(By.linkText("About"));
Actions builder = new Actions(driver);
builder.keyDown(Keys.SHIFT).click(link).keyUp(Keys.SHIFT).build().perform();
I'm trying to make a clickable button which links to www.snaptrude.com. But clicking on the button, it throws an error which goes like:
The left-hand side of an assignment must be a
variable
- Syntax error on token "home", invalid
AssignmentOperator
Here's the code I'm referencing to
package snaptrude;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyProject {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver","C:\\Users\\Kunal\\Desktop\\Selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "http://www.snaptrude.com";
// launch Chrome and direct it to the Base URL
driver.get(baseUrl);
driver.findElement(By.name("email")).sendKeys("snaptrude#snaptrude.com");
WebElement f = driver.findElement(By.id("login"));
WebElement a = driver.findElement(By.xpath("//*[#id="home"]/div/div/div/div/div/span[2]/h2/strong"));
a.click();
}
}
You have done syntax error :- please refer below code
WebElement a = driver.findElement(By.xpath("//*[#id='home']/div/div/div/div/div/span[2]/h2/strong")
Team,
I am stuck in one issue.
I have two classes One where i have created a plain login functionality using constructor and in second one i am instantiating the login.
The issue is the test case is terminating immediately.
Please help me out
I am giving the below code.
Is there any problem with initialisation.
public Userlogin(String username, String brand, WebDriver driver) {
WebElement user=driver.findElement(By.id("UserNameInputText"));
user.sendKeys(username);
Select select=new Select(driver.findElement(By.id("Brand")));
select.selectByValue(brand);
WebElement login_button=driver.findElement(By.id("CmdLogin"));
login_button.submit();
String actual_title=driver.getTitle();
String expected_title="VSS 4";
if(!(actual_title.matches(expected_title)))
{
Assert.assertFalse(false);
driver.quit();
}
WebElement cancel=driver.findElement(By.id("Cancel"));
if(!driver.findElements(By.id("Cancel")).isEmpty())
{
cancel.click();
}
}
}
I am calling login in below code
package testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import vsslogin.Userlogin;
public class messageboard extends Userlogin {
public messageboard(String username, String brand, WebDriver driver) {
super(username, brand, driver);
// TODO Auto-generated constructor stub
}
WebDriver driver;
#Test
void messageboard()
{
System.setProperty("webdriver.chrome.driver", "C:/Eclipse/chromedriver.exe");
driver=new ChromeDriver();
driver.get("http://153.112.61.197/vss_connect_testr1/Login/Login.aspx?nextview=Welcome");
driver.manage().window().maximize();
Userlogin login=new Userlogin("TYP40US","Mack",driver);
}
}
Try to add System.out.println() statement in your code mentioned below and RUN.
If you have seen the "Close Browser" Message in Console. Tile of the Page is Not Matched with your expected_title.
Line : driver.quit(); is terminates your browser.
if(!(actual_title.matches(expected_title)))
{
System.out.println("Close Browser");
Assert.assertFalse(false);
driver.quit();
}
I'm a learner, i'm trying to compose a mail from new gmail, but not able to enter id's in to list after compose window opens,tried switching the frame method but was not successfull in locating the to field itself, can you please help me in this.
Thanks.
This blog contains a lot of examples that are doing just that:
http://shilpamynampati.blogspot.com/
Is this what you need?
Here is the example of composing a mail from GMail account.
package testCase;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.File;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class GmailFileUpload
{
WebDriver driver = null;
WebElement element = null;
#Before
public void setUp() throws Exception
{
File file = new File("G:\\Selenium\\All_Jars\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
driver.manage().window().maximize();
}
#Test
public void test() throws InterruptedException, AWTException
{
driver.get("https://www.google.co.in");
driver.findElement(By.linkText("Sign in")).click();
driver.findElement(By.id("Email")).sendKeys("aavinashpande#gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("your gmail password");
driver.findElement(By.id("signIn")).click();
driver.findElement(By.linkText("Gmail")).click();
driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click(); //Click on Compose button
Thread.sleep(5000);
driver.findElement(By.xpath("//textarea[#name='to']")).sendKeys("aavinashpande#gmail.com"); // write mail id to whom do you want to send an email
driver.findElement(By.xpath("//input[#name='subjectbox']")).sendKeys("want to say Hello"); // write subject
element = driver.findElement(By.xpath("//div[#class='Ar Au']//div"));
element.click();
element.sendKeys("Hi Avinash"); //type in message body
driver.findElement(By.xpath("//div[contains(text(),'Send')]")).click(); //click on send button
}
}
package basic;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class mozilaProj {
public static void main(String[] args) throws InterruptedException {
String path = "D:\\harsh\\Selenium data\\geckodriver.exe"; //location where your driver is placed.
System.setProperty("webdriver.gecko.driver", path);
WebDriver wd=new FirefoxDriver();
wd.navigate().to("http://accounts.google.com");
wd.findElement(By.id("identifierId")).sendKeys("Your mail ID");
wd.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div/content/span")).click(); // Click on next
Thread.sleep(4000); //wait needed here To get the password page.
wd.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div[1]/div/form/content/section/div/content/div[1]/div/div[1]/div/div[1]/input")).sendKeys("Email Password"); // click on next
wd.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div/content")).click(); //If your page redirect to google security page.
Thread.sleep(2000);
wd.findElement(By.cssSelector(".gb_mf > path:nth-child(1)")).click(); //click on google apps tab.
wd.findElement(By.cssSelector("#gb23 > span:nth-child(5)")).click(); //click on gmail icon.
wd.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div/div[1]/div/div")).click(); // click on compose
WebDriverWait wait = new WebDriverWait(wd, 20); // implement wait here to load all data.
wait.until(
ExpectedConditions.visibilityOfElementLocated(By.name("to")));
wd.findElement(By.name("to")).sendKeys("recepient mail ID");
wd.findElement(By.id(":mx")).sendKeys("Subject of mail");
wd.findElement(By.id(":o0")).sendKeys("Mail body "); // type mail body
wd.findElement(By.id(":mn")).click(); // Send button click
Thread.sleep(8000); // Apply wait for sending mail
wd.close();
}
}