Gmail - Web Driver Automate - xpath

Here is the code I am trying to automate Gmail through Web Driver.
I found something weird. Whenever I am commenting out the line to find the Password (Driver.findElement(By.xpath(".//*[#id='Passwd']")).sendKeys("SRS");)
then Web Driver is successfully clicking on "Sign In" button
but when I un-comment the line
(Driver.findElement(By.xpath(".//*[#id='Passwd']")).sendKeys("SRS");)
then Web Driver is not able to click on Sign In button also and it gives the error message that Unable to find the Password Xpath however still its on Email id Screen only
Here is the attached screenshot
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Gmail {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver Driver = new FirefoxDriver();
Driver.get("https://www.google.com/gmail/about/");
Driver.findElement(By.xpath("html/body/nav/div/a[2]")).click();
Driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
//Enter the Gmail ID
Driver.findElement(By.xpath(".//*[#id='Email']")).sendKeys("RK12#gmail.com");
Driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//Click on Next Button
Driver.findElement(By.xpath(".//*[#id='next']")).click();
Driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Driver.findElement(By.xpath(".//*[#id='Passwd']")).sendKeys("SRS");
//Driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
}
}

1. You are giving wrong email id, hence Google validated it and shown that the error message as "Sorry, Google doesn't recognize that email".
So, enter valid email id in order to proceed to password page.
2. Here, with the code in the question, gives ElementNotVisibleException, hence added ExpectedConditions.visibilityOfElementLocated to make sure that password field is loaded before sending the keys.
Updated code:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Gmail {
public static void main(String[] args) {
WebDriver Driver = new FirefoxDriver();
Driver.get("https://www.google.com/gmail/about/");
Driver.findElement(By.xpath("html/body/nav/div/a[2]")).click();
Driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
//Enter the Gmail ID
Driver.findElement(By.xpath(".//*[#id='Email']")).sendKeys("rbnaveen558#gmail.com");
Driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//Click on Next Button
Driver.findElement(By.xpath(".//*[#id='next']")).click();
Driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(Driver, 10);
WebElement pwd = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
pwd.sendKeys("SRS");
//Driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
}
}

Please add "sign in" button also,i tested this.This is working fine.
driver.findElement(By.xpath(".//*[#id='Passwd']")).sendKeys("SRS");
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
//click to sign in
driver.findElement(By.id("signIn")).click();

Related

load chromedriver in spring boot

I'm running a spring boot application and I'm trying to get the chromedriver not load from my local directory but rather from the project resources folder. I have my chromdriver.exe in resources/chromedriver.exe but I'm not sure how can I load it.
I tried this didn't work. Tried filepath to be "resources/chromedriver.exe" didn't work as well
String filePath = ClassLoader.getSystemClassLoader().getResource("resources/chromedriver.exe").getFile();
System.out.println(filePath);
System.setProperty("webdriver.chrome.driver", filePath)
If you are using Spring, you could try this:
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
Resource resource = new ClassPathResource("chromedriver.exe");
String filePath = resource.getFile().getPath();
System.out.println(filePath);
System.setProperty("webdriver.chrome.driver", filePath);
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.junit.Test;
public class GettingStarted {
#Test
public void testGoogleSearch() throws InterruptedException {
// Optional. If not specified, WebDriver searches the PATH for chromedriver.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com/");
Thread.sleep(5000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
Thread.sleep(5000); // Let the user actually see something!
driver.quit();
}
}
https://sites.google.com/a/chromium.org/chromedriver/getting-started
For example in Windows if chromedriver.exe is in C:/chromium use:
System.setProperty("webdriver.chrome.driver", "C:\\chromium\\chromedriver.exe");

Selenium webdriver script not able to log in into magento Panel

I am trying to log in to Magento Admin panel via Selenium web Driver script but its not working .
It identifies login button and clicks on login button but it is not getting in to next page once log in successful.
userid : magadmin
Password : Lean5226
when you try it with iDE it is working
Website :
54.201.104.110/magento/env-ee/index.php/admin
Here is below script :
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:/ChromeDriver/chromedriver.exe");
WebDriver driver=new ChromeDriver();
System.out.println("Fire fox started");
String baseUrl = "http://54.201.104.110/";
driver.get(baseUrl + "/magento/env-ee/index.php/admin/");
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys("magadmin");
driver.findElement(By.id("login")).clear();
driver.findElement(By.id("login")).sendKeys("Lean5226");
Thread.sleep(3000);
String att=driver.findElement(By.cssSelector("input.form-button")).getAttribute("value");
System.out.println(att);
WebDriverWait wait = new WebDriverWait(driver, 15);
driver.findElement(By.cssSelector("input.form-button")).click();
System.out.println("Waiting");
Thread.sleep(3000);
driver.close();
This post is a little old, but in the event that other people find it interesting you can do this using Magium with a fairly simple command. The following code is from one of the blog posts on logging in to the Magento admin IU
class LoginTest extends \Magium\Magento\AbstractMagentoTestCase
{
public function testLogin()
{
$this->getAction(\Magium\Magento\Actions\Admin\Login\Login::ACTION)->login();
}
}
There is some additional information on how to configure it on the blog post.

second scenario in my cucumber feature file is not executing

I'm trying to learn Cucumber with selenium java . have written two scenario's , when i run my feature file which contains two scenarios , only scenario #1 is executing , for scenario #2 its throwing Java null pointer exception
Feature: POC of my framework works
Scenario: Login test
Given I navigate to the Bugzilla website
When I click on login
And I enter the values
Then I check to see if i was successfully loged in or not
Scenario: File a bug test
Given I navigate to the File a bug page
When I click on widgets
And I enter the bug details
Then Bug should be submited succefully
My step definition file :
package cucumber.features;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class StepDefinitions1 {
protected WebDriver driver;
protected String baseUrl;
// Scenario 1
#Given("^I navigate to the Bugzilla website$")
public void I_navigate_to_the_Bugzilla_website() throws Throwable {
driver = new FirefoxDriver();
baseUrl ="https://landfill.bugzilla.org/bugzilla-4.4-branch/index.cgi";
driver.get(baseUrl);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#When("^I click on login$")
public void I_click_on_login() throws Throwable {
driver.findElement(By.id("login_link_top")).click();
}
#And("^I enter the values$")
public void I_enter_the_values() throws Throwable {
driver.findElement(By.id("Bugzilla_login_top")).clear();
driver.findElement(By.id("Bugzilla_login_top")).sendKeys("jeevan.anekal#gmail.com");
driver.findElement(By.id("Bugzilla_password_top")).clear();
driver.findElement(By.id("Bugzilla_password_top")).sendKeys("testuser#123");
driver.findElement(By.id("log_in_top")).click();
}
#Then("^I check to see if i was successfully loged in or not$")
public void I_check_to_see_if_i_was_successfully_loged_in_or_not() throws Throwable {
System.out.println("Login Successfull");
}
// Scenario 2
#Given("^I navigate to the File a bug page$")
public void I_navigate_to_the_File_a_bug_page() throws Throwable {
driver.findElement(By.id("enter_bug")).click();
}
#When("^I click on widgets$")
public void I_click_on_widgets() throws Throwable {
driver.findElement(By.linkText("Widgets")).click();
}
#And("^I enter the bug details$")
public void I_enter_the_bug_details() throws Throwable {
new Select(driver.findElement(By.id("bug_severity"))).selectByVisibleText("trivial");
new Select(driver.findElement(By.id("cf_drop_down"))).selectByVisibleText("---");
new Select(driver.findElement(By.id("rep_platform"))).selectByVisibleText("Macintosh");
new Select(driver.findElement(By.id("op_sys"))).selectByVisibleText("Mac OS X 10.0");
driver.findElement(By.id("short_desc")).clear();
driver.findElement(By.id("short_desc")).sendKeys("OS crashed");
driver.findElement(By.id("comment")).clear();
driver.findElement(By.id("comment")).sendKeys("Os debugging issue");
}
#Then("^Bug should be submited succefully$")
public void Bug_should_be_submited_succefully() throws Throwable {
System.out.println("Bug submitted successfully");
}
}
Looks like you are not calling the browser and navigating to the page in scenario 2
You need do do something similar to this
#Given("^I navigate to the File a bug page$")
public void I_navigate_to_the_File_a_bug_page() throws Throwable {
driver = new FirefoxDriver();
baseUrl ="https://landfill.bugzilla.org/bugzilla-4.4-nch/index.cgi";
driver.get(baseUrl);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("enter_bug")).click();
enter code here
}
or use #Before hooks to initialize the browser
I think I kind get your problem here. I had encountered similar problem before.
So when running the feature file, either by clicking on the run button from the bar or right click in the feature file then run..., take a look at what it says when actually clicking it. Because if you hover over a certain scenario and click run, it will only run that scenario, not the whole feature file where there is more than 1 scenarios.

How to write adb commands inside a Android application?

I want to know how to execute adb commands from within my code. For e.g I want to push a file inside the adb shell and I write this:
package org.example.adbshell;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Process process;
try {
process = Runtime.getRuntime().exec("adb push C:/Users/Savio/Desktop/savio.xml /storage/sdcard0/");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
I should be able to see the savio.xml inside the /storage/sdcard0 folder. But for some reason I am unable to see the file. I am guessing that the command is not getting executed. What am I doing wrong here?
There are three type of command
System command // In this case below code is working. like mv/edit/cp/cd etc
Non routed command
Rooted command // You need to routed device.
Try this
Process process Runtime.getRuntime().exec("your command");
//or
Process process Runtime.getRuntime().exec("/path/your_command");
Its work for me of copying file :
Process process Runtime.getRuntime().exec("/system/bin/mv my_file_path");
You can read output data with the help of process object
// Use buffer reader for the same.
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
There is no ADB command on Android's side. ADB is a part of Android SDK only and is always run on computer not on Android.
Also, Android knows nothing about C:/Users/Savio/Desktop/savio.xml as it is not a file in its filesystem.
You should rather implement desktop application with HTTP (or similar) server and use it to download and upload files from Android to PC. You can start with NanoHTTP, a lightweight HTTP implementation.
Or you can use server for communication between Android and computer and run ADB command on that computer based upon request from Android.

Click() on HtmlArea and nothing changes, why?

I'm trying my hand at HtmlUnity and have ran into trouble when I try to click an area with javaScript.
Here is the code:
import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlArea;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlMap;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class ToPost {
/**
* #param args
* #throws IOException
* #throws MalformedURLException
* #throws FailingHttpStatusCodeException
*/
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
HtmlPage page;
final WebClient webClient = new WebClient();
page = webClient.getPage("http://www.hidrografico.pt/previsao-mares.php");
System.out.println(page.getTitleText());
HtmlPage pagePortoLeixoes = setPort(page, "362,64,440,90");
System.out.println("Are they the same? "+page.asXml().equals(pagePortoLeixoes.asXml()));
}
private static HtmlPage setPort(HtmlPage page, String coordinatesPort) throws IOException {
HtmlMap map = page.getHtmlElementById("FPMap1");
Iterable<HtmlElement> childAreas = map.getChildElements();
HtmlArea tempArea;
for (HtmlElement htmlElement : childAreas) {
tempArea = (HtmlArea) htmlElement;
if(tempArea.getCoordsAttribute().equals(coordinatesPort)){
System.out.println("Found Leixoes! --> "+ tempArea.asXml());
return tempArea.click();
}
}
return null;
}
}
I don't show it here but I double-check in my full code that I'm really not in the page I want.
What is happening? Why doesn't the click work?
HtmlUnit .click() often works poorly when "complex" javascript is involved.
http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/html/HtmlElement.html#click()
Simulates clicking on this element, returning the page in the window that has the focus after the element has been clicked. Note that the returned page may or may not be the same as the original page, depending on the type of element being clicked, the presence of JavaScript action listeners, etc
In this case, you'll have to find another way to catch the data.
What i did see is that using .rss links, it gives you direct links to cities ...
eg : http://www.hidrografico.pt/previsao-mares-aveiro.php
Another way would have been to forge a POST request (check for exemple with Httpfox which requests are done when you're stuck getting a page)

Resources