Selenium and browserstack using java test not working - selenium

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?

Related

Getting InvalidArgumentException

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

Error when running code as java application

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?

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

Getting Null Pointer Exception with Extent Reports

I m trying to run a small test program with Extent Reports but getting Null Pointer Exception. If i am running this code, without using Extent Report functionality it's working fine. Kindly help.
Below is my code-
package selenium.ExtentReports;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import io.github.bonigarcia.wdm.WebDriverManager;
public class ExtentReportDemo {
ExtentReports extent;
WebDriver driver;
#BeforeTest
public void config() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
String path = System.getProperty("user.dir") + "//Reports//index.html";
ExtentSparkReporter reporter = new ExtentSparkReporter(path);
reporter.config().setReportName("Web Automation Results");
reporter.config().setDocumentTitle("Test Results");
ExtentReports extent = new ExtentReports();
extent.attachReporter(reporter);
extent.setSystemInfo("Tester", "Gaurav Singh");
}
#Test
public void initialDemo() {
extent.createTest("Initial Demo");
driver.get("https://rahulshettyacademy.com");
System.out.println(driver.getTitle());
extent.flush();
}
}
Error which i am getting
FAILED: initialDemo
java.lang.NullPointerException
at selenium.ExtentReports.ExtentReportDemo.initialDemo(ExtentReportDemo.java:38)
I have just instantiate the extent variable outside the clas. And it's working for me now.
ExtentReports extent = new ExtentReports();
ExtentReports extent = new ExtentReports();
Don't initialize in #BeforeTest methods once you declared it Publicly. Instead Use :
extent = new ExtentReports();

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.