Composing a mail from Selenium WebDriver - selenium-webdriver

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

Related

i am unable to type text in loginpage by using sendkeys

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);*/

How to select specific flight record on flight selection page, using Selenium webdriver

I am able to navigate to Flight Reservation page on website - http://www.spicejet.com/. Code is given below:
Code:
package Flight_Reservation;
import java.util.List;
import java.util.concurrent.TimeUnit;
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.support.ui.Select;
public class NewFlight_OneWay {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\Selenium_Practice\\EXEs\\geckodriver-v0.10.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.spicejet.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#id='ctl00_mainContent_rbtnl_Trip_1']")).click();
//Select origin
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).sendKeys("DEL");
driver.findElement(By.linkText("Delhi (DEL)")).click();
//Select destination
driver.findElement(By.id("ctl00_mainContent_ddl_destinationStation1_CTXT")).sendKeys("BOM");
driver.findElement(By.linkText("Mumbai (BOM)")).click();
WebElement DateWidget = driver.findElement(By.id("ui-datepicker-div"));
List<WebElement> columns = DateWidget.findElements(By.tagName("td"));
for (WebElement cell: columns)
{
if (cell.getText().equals("24"))
{
cell.findElement(By.linkText("24")).click();
break;
}
}
Select AdultDropdown = new Select(driver.findElement(By.id("ctl00_mainContent_ddl_Adult")));
AdultDropdown.selectByValue("2");
Select ChildrenDropdown = new Select(driver.findElement(By.id("ctl00_mainContent_ddl_Child")));
ChildrenDropdown.selectByValue("1");
Select InfantDropdown = new Select(driver.findElement(By.id("ctl00_mainContent_ddl_Infant")));
InfantDropdown.selectByValue("1");
Select CurrencyDropdown = new Select(driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency")));
CurrencyDropdown.selectByValue("INR");
driver.findElement(By.id("ctl00_mainContent_btn_FindFlights")).click();
}
}
Question:
On flight selection page, I want to select flight record radio button with flight name 'SG 161'. Please let me know how can I achieve that?
Use following code to click on required radio-button:
driver.findElement(By.xpath('//input[#type="radio"][contains(#value, "SG~ 161")]')).click();

Openbravo ERP application using selenium webdriver but not able to find any web element

When I'm trying to run this code, I am unable to find any web element
package PremisesManagement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
public class openbravo {
#Test
public void openbravoLoginTest() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
WebDriver d = new ChromeDriver();
d.get("http://119.81.222.91:8080/camps/security/Login_FS.html");
Thread.sleep(3000);
d.findElement(By.id("user")).sendKeys("xyz");
WebDriverWait wait = new WebDriverWait(d, 120);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("//button[#id='buttonOK']")));
d.findElement(By.xpath("//button[#id='buttonOK']")).click();
}
}
Actually located elements are inside a frame with id paramFrame1, So you need to switch this frame before finding element as below :-
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://119.81.222.91:8080/camps/security/Login_FS.html");
WebDriverWait wait = new WebDriverWait(driver, 10);
//First switch to frame with id paramFrame1
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("paramFrame1"));
//Now find the element
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("user"))).sendKeys("xyz");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("buttonOK"))).click();

Using driver.switchTo() is automatically clearing the cookie (Selenium web driver-Java)

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

unable to launch internet explorer in TestNG

I am new in TestNG. This is my code which I tried in eclipse, but there is a problem occurring in launching internet explorer.
The error it is giving is
org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (WARNING: The server did not provide any stacktrace information)
This is the complete code....
package com.tcs.medmantra;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Registration {
WebDriver driver;
WebElement element;
WebElement element2;
WebDriverWait waiter;
#Test(priority = 1)
public void register_With_Cash() throws RowsExceededException, BiffException, WriteException, IOException
{
driver=new InternetExplorerDriver();
driver.get("https://172.25.155.250/loginpage.aspx");
//((JavascriptExecutor)driver).executeScript("window.resizeTo(1366, 768);");
waiter = new WebDriverWait (driver, 40);
driver.findElement(By.id("txtuname")).sendKeys("122337");
driver.findElement(By.name("txtpwd")).sendKeys("Tcs!#345");
driver.findElement(By.id("btnsubmit")).click();
sleep(25000);
//print URL
String url = driver.getCurrentUrl();
System.out.println(url);
}
#BeforeTest
public void beforeTest() {
File file = new File("D:\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
}
#AfterTest
public void afterTest() {
driver.quit();
}
}
As the error says you need set the protected mode same for all zones, either enabled or disabled. Preferred would be enabled. See here
Change your code to include the path for InternetExplorerDriver.
File file = new File("C:/Selenium/iexploredriver.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();

Resources