Getting InvalidArgumentException - selenium

package trails2110;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Demo01 {
WebDriver driver;
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "D:\\Eclipse\\Drivers\\chromedriver.exe");
System.out.println("1");
driver= new ChromeDriver();
System.out.println("2");
}
#After
public void tearDown() throws Exception {
driver.close();
}
#Test
public void test() {
String url= "www.hotstar.com";
System.out.println("3");
driver.get(url);
System.out.println("4");
String Title=driver.getTitle();
System.out.println(Title);
}
}
In Console till 3 its getting printed.
I have latest version of chrome and latest version of chrome driver for it.
i tried with multiple drivers didnt work.
am i missing something?
I am able to route to the url with the following code
System.setProperty("webdriver.chrome.driver", "D:\\Eclipse\\Drivers\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
String url = "https://www.google.com";
String script = "window.location = \'"+url+"\'";
((JavascriptExecutor) driver).executeScript(script);
any reasons its not happening with get() method and happening with JavascriptExecutor ?

You should declare the URL with its protocol. So
String url= "www.hotstar.com";
url = "https://" + url

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

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

Selenium and browserstack using java test not working

I am trying to run this selenium test code from Browserstack but I cannot get pass the error I am getting.
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
public class JavaSample {
public static final String USERNAME = "username";
public static final String AUTOMATE_KEY = "key";
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "#hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws Exception {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("browser", "IE");
caps.setCapability("browser_version", "7.0");
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "XP");
caps.setCapability("browserstack.debug", "true");
WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("BrowserStack");
element.submit();
System.out.println(driver.getTitle());
driver.quit();
}
}
The error I am getting is
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
The method sendKeys(CharSequence...) from the type WebElement refers to the missing type CharSequence
at mypackage.JavaSample.main(JavaSample.java:30)
I have the selenium library and java library on the project. I am using eclipse.
A similar issue was reported here- Error when using sendKeys() with Selenium WebDriver Java.lang.CharSequence cannot be resolved. Can you check if it is something similar?

java.lang.NullPointerException at feature.StepDefinitions cucumber

I'm getting a NullPointerException as follows:
Feature: Login action
Scenario:
Successful Login with Valid Credentials # C:/Users/chaitanya/workspace/cucumber2/src/feature/myfeature.feature:3
Given User is on Home Page # StepDefinitions.User_is_on_Home_Page()
When User enters UserName and Password # StepDefinitions.User_enters_UserName_and_Password()
java.lang.NullPointerException
at feature.StepDefinitions.User_enters_UserName_and_Password(StepDefinitions.java:25)
at ?.When User enters UserName and Password(C:/Users/chaitanya/workspace/cucumber2/src/feature/myfeature.feature:5)
Then Message displayed Login Successfully # StepDefinitions.Message_displayed_Login_Successfully()
Code:
package feature;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class StepDefinitions {
public static WebDriver driver;
#Given("^User is on Home Page$")
public void User_is_on_Home_Page() throws Throwable {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://opensource.demo.orangehrmlive.com/");
}
#When("^User enters UserName and Password$")
public void User_enters_UserName_and_Password() throws Throwable {
driver.findElement(By.name("txtUsername")).sendKeys("admin");
driver.findElement(By.xpath("//input[#id='txtPassword']")).sendKeys("admin");
driver.findElement(By.name("Submit")).click();
Thread.sleep(3000);
}
#Then("^Message displayed Login Successfully$")
public void Message_displayed_Login_Successfully() throws Throwable {
System.out.println("login completed");
}
}
In User_is_on_Home_Page() you're using a local variable named driver within that method. You're not setting the static driver that your other methods are using. As a result, when they reference driver it is still null.
The solution is to change:
public static WebDriver driver;
to:
public static final WebDriver driver = new FirefoxDriver();
and remove the WebDriver driver = new FirefoxDriver(); line from User_is_on_Home_Page() so that it likewise refers to the static instance.
Alternatively, instantiate the static driver instance lazily. Replace:
WebDriver driver = new FirefoxDriver();
in User_is_on_Home_Page() with:
if (driver == null) {
driver = new FirefoxDriver();
}