Error when running code as java application - selenium

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SELintroduction {
public static void main(String[] args) {
//Invoking Browser
//Chrome - ChromeDriver .exten-.Methods close get
//Firefox- Firefoxdriver ->methods close get
//safari SaariDrier ->methods close get
//WebDriver close get
//WebDriver methods + class methods
// chromedriver.exe->Chrome browser
System.setProperty("webdriver.chrome.driver","C:\\Users\\user\\Downloads\\chromedriver_win32.exe");
//webdriver.chrome.driver->value of path
WebDriver driver = new ChromeDriver();
driver.get("http://rahulshettyacademy.com");
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());
what is the wrong here coding shows error?

Related

WebElement Click() showing NullPointerException in Selenium

I was trying Selenium automation testing in https://www.yatra.com/etw-desktop/ . My objective was to click an Image Button named 'Asia' which will redirect to another page (Images attached). I copied the full XPath and tried but I am getting a NullPointerException. Please give some suggestions since I didn't find anything wrong with my code.
package com.stackroute.SeleniumProject;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
/**
* JUnit project.
*/
public class Yatra {
public static WebDriver driver = null;
#FindBy(xpath = "/html/body/my-app//app-drawer-layout/app-header-layout/iron-pages/my-home//div/div/div/div/paper-material[2]/div[1]/div/a[2]/div[4]")
WebElement Asia;
#BeforeClass
public static void setup() {
String chromePath = System.getProperty("user.dir") + "/lib/chromedriver.exe";// directory of chrome driver
System.setProperty("webdriver.chrome.driver", chromePath);
driver = new ChromeDriver();
}
#AfterClass
public static void close() throws InterruptedException {
driver.close();
}
#Test
public void test1() throws InterruptedException {
driver.manage().window().maximize();
driver.get("https://www.yatra.com/etw-desktop/");
driver.manage().timeouts().implicitlyWait(4000, TimeUnit.MILLISECONDS);
Thread.sleep(5000);
// the error is in the below line Asia.click()
Asia.click();
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
Thread.sleep(2000);
Assert.assertEquals("page not found", "https://www.yatra.com/etw-desktop/city-list", driver.getCurrentUrl());
}
}
Image showing the element to be clicked
Image of web page after click operation
You are using PageFactory(from page object model), so you will need to init() the webelements.
For this you will need to import
org.openqa.selenium.support.PageFactory;
An before starting the test, you will need to initialise the elements:
PageFactory.initElements(driver, this) // where you pass the driver and this class to know which webelements to start.
You can take a look here to understand the approach and another POM framework easier to understand TUTORIAL

I'm not getting title of the page,i tried in firefox as well as in chrome

I'm not getting title of the page,i tried in firefox as well as in chrome.
This is my package
package begin;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Title {
WebDriver driver;
public void tite()
{
driver=new FirefoxDriver();
System.setProperty("webdriver.firefox.driver","C:/selenium-java-3.0.0-beta3/Latest selenium/geckodriver.exe");
driver.get("http://newtours.demoaut.com/");
String titleofthepage=driver.getTitle();
System.out.println(titleofthepage);
}
public static void main(String[] args)
{
Title obj1=new Title();
obj1.tite();
}
}
Need to add wait attributes to driver element.
After creating driver add implicit wait
System.setProperty("webdriver.firefox.driver","geckodriverpath");
driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
You are finding page title immediately after launching web page. Here it will wait 30 secs before finding any element to web page.
Set the property before driver initialization So your code should be :
package begin;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Title {
WebDriver driver;
public void tite()
{
System.setProperty("webdriver.firefox.driver","C:/selenium-java-3.0.0-beta3/Latest selenium/geckodriver.exe");
driver=new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
String titleofthepage=driver.getTitle();
System.out.println(titleofthepage);
}
public static void main(String[] args)
{
Title obj1=new Title();
obj1.tite();
}
}

Selenium - The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files

Selenium - The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
I am new to selenium, could any one please help on this?
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.ie.driver",System.getProperty("user.dir")+ "\\iedriver\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("https://accounts.google.com/ServiceLogin?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1#identifier");
WebElement username = driver.findElement(By.id("Email"));
username.sendKeys("selenium");
}
Selenium - 3.3.1
Java - 1.8
Eclispe - Indigo
Compiler - 1.7
Use this code with Chrome Driver/Browser:
package demo;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestAnyURLMain {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[#id='gbw']/div/div/div[1]/div[1]/a")).click();
driver.findElement(By.id("Email")).sendKeys("ABC");
}
}
Let me know if it helps you.

Launching URL using Selenium gecko driver

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CheckoutFlow {
public static void main(String[] args) {
// TODO Auto-generated method s
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe");
WebDriver driver= new FirefoxDriver();
driver.get("http://google.com");
}
}
While launching the URL - from Java code below error is coming
"Server not Found"
Your code is perfectly fine. This might be the issue with Firefox version,
firefox version(I'm using) 52.0.1.
If still it is not working then try to switch to chrome instead of firefox.
Chrome Code :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class CheckoutFlow {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://google.com");
driver.manage().window().maximize();
driver.quit();
}
}

Exception in thread "main" java.lang.NullPointerException in selenium when trying to run a test case

Below is my simple testcase program:
package mypackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class myclass {
public WebDriver driver;
public static void main(String[] args) {
myclass dr= new myclass();
dr.start();
dr.select();
}
public void start(){
WebDriver driver= new FirefoxDriver();
driver.get("https://www.google.co.in/");
}
public void select(){
driver.findElement(By.linkText("Gmail")).click();
}
}
but it throws the below error everytime i run it:
Exception in thread "main" java.lang.NullPointerException
at mypackage.myclass.select(myclass.java:26)
at mypackage.myclass.main(myclass.java:15)
The browser gets launched and the google home page is also displayed but the next action of selecting the gmail link doesnt happen and the error appears.
**tried this on different browsers(i.e. chrome) but error still persists
please help me with this i am new to selenium..
Look up "variable scope" in Java.
This line:
driver.findElement(By.linkText("Gmail")).click();
is referencing:
public WebDriver driver;
which is never set as anything.
This should fix that error:
public void start(){
driver= new FirefoxDriver();
driver.get("https://www.google.co.in/");
}
Also, class names should start with a capital letter.
just remove the "WebDriver" instance from the start() method which is already declared above ,if you use it then "WebDriver" declare globally is not in current scope of method start()
package mypackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class myclass {
public WebDriver driver;
public static void main(String[] args) {
myclass dr= new myclass();
dr.start();
dr.select();
}
public void start(){
driver= new FirefoxDriver();
driver.get("https://www.google.co.in/");
}
public void select(){
driver.findElement(By.linkText("Gmail")).click();
}
}