Unable to Launch browser - selenium

public class LaunchBrowser {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
System.setProperty("webdriver.gecko.driver", "/Users/spectra/Drivers/geckodriver");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
driver.get("https://www.seleniumhq.org");
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type
FirefoxDriver cannot be resolved to a type
at com.simplilearn.day2.oops.LaunchBrowser.main(LaunchBrowser.java:16)

setProperty before driver initialization which should be like
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "/Users/spectra/Drivers/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
driver.get("https://www.seleniumhq.org");
}

Related

How to connect Selenium and AdBlock?

`
public class CucumberHooks {
public static void main(String[] args) {
}
protected static WebDriver driver;
#Before
public void setup() throws IOException {
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("extension_5_3_2_0.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
String browser = ReadPropertyFileSingleton.getInstance().getProp("browser");
driver = Util.setEnvironmentAndGetDriver(browser);
assert driver != null;
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
public static WebDriver getDriver() {
return driver;
}
#After
public void close() {
driver.quit();
}}
The code where I added adblock extensions via crx file
When I run tests through a feature file, a regular browser without a blocker is launched
the browser is also launched already with a blocker, but it does not see the steps
what could be the problem and is it possible to use adblock this way?

Getting Null pointer exception when declaring Webdriver as null outside the method and also declaring webdriver inside the method

Can some one explain why i am getting Null pointer exception when code execution reaches login() method in below script
public class TC_01_CreateEmployee {
WebDriver driver=null;
public void launchBrowser() throws Exception
{
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
}
public void login()
{
driver.get("******");
driver.findElement(By.id("txtUsername")).sendKeys("****");
driver.findElement(By.id("txtPassword")).sendKeys("****");
driver.findElement(By.id("btnLogin")).click();
}
WebDriver driver=new ChromeDriver(); - This driver has a scope only within the method. The driver object which is used on the login method is still null. I am not sure why you would need 2 driver objects. you have 2 options to solve this,
public void launchBrowser() throws Exception
{
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
this.driver = driver;
Thread.sleep(2000);
}
Or
public void launchBrowser() throws Exception
{
driver=new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
}
It is likely that the launchBrowser() method is not being called before the login() method is being called.
An easy way around this is to define a getDriver() method that calls launchBrowser if driver is null.
private WebDriver getDriver() {
if (driver == null) {
launchBrowser();
}
return driver;
}
Then your login method looks like:
WebDriver driver = getDriver();
driver.get("******");
driver.findElement(By.id("txtUsername")).sendKeys("****");
driver.findElement(By.id("txtPassword")).sendKeys("****");
driver.findElement(By.id("btnLogin")).click();

Null pointer exception on testng

I have written the below code and getting the java null pointer exception.
please help what is the problem.
public class testngbasics {
WebDriver driver;
#BeforeMethod
public void setbrowser() {
System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
driver.get("http://demo.guru99.com/v4/");
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
}
#Test
public void login1() {
driver.findElement(By.xpath("//input[#name='uid']")).sendKeys("mngr212595");
driver.findElement(By.xpath("//input[#name='password']")).sendKeys("EgebYpy");
driver.findElement(By.xpath("//input[#value='LOGIN']")).click();
System.out.println(driver.getTitle());
}
#AfterMethod
public void closebrowser()
{
driver.close();
}
}
I am getting the error:
java.lang.NullPointerException it is only going to #beforemethod
annotation and not going to #test and #aftermethod
As you have already defined a global instance of WebDriver as driver in the line:
WebDriver driver;
You don't have to create another instance of WebDriver again as in:
WebDriver driver= new ChromeDriver();
And you need to replace this line as:
driver = new ChromeDriver();

How to resolve null pointer exception in selenium when page factory method is used

I have 3 classes - one for page locator, one for page action and the other one as script to execute the function. am getting nullpointer exception in main scripts where the function is called. Can anyone help me out, please!!!!.
The following are the code :
HomePageLocator.page
public class HomePageLocator{
WebDriver driver;
public HomePageLocator(WebDriver driver)
{
this.driver= driver;
}
#FindBy(xpath="//*[#id='header']/div[2]/div/div/nav/div[1]/a")
public WebElement signIn;
}
HomePageAction.page
public class HomePageAction{
public WebDriver driver;
public HomePageLocator homepageor;
public HomePageAction() {
this.homepage = new HomePageLocator(driver);
PageFactory.initElements(driver, this.homepage);
}
public void login() {
homepageor.signIn.click();
}
BaseTestCase.java
public class BaseTestCase {
public static Logger log = Logger.getLogger("devpinoyLogger");
public static void main(String[] args) throws Throwable {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\src\\test\\resources\\Executables\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://automationpractice.com/index.php");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Home page validation
HomePageAction homepageaction= new HomePageAction();
homepageaction.login();
}
Note : Am getting exception in line (homepageaction.login();)
the following is the exception logs :
Exception in thread "main" java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy3.click(Unknown Source)
at com.way2.Pages.actions.HomePage.login(HomePageAction.java:31)
at com.way2.Testcases.BaseTestCase.main(BaseTestCase.java:35)
You are creating the driver in main class, but not passing it to homepageAction
public static void main(String[] args) throws Throwable {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\src\\test\\resources\\Executables\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Try to pass driver as
HomePageAction homepageaction= new HomePageAction(driver);
this.driver =driver

Facing Issue "Cannot find class in classpath" in selenium

I am learning selenium, as part of i was trying to execute the below program. But i was getting the error " Cannot find class in classpath:practiseAutomation". The code is below:
public class practiseAutomation {
public WebDriver driver;
String baseurl="http://www.ticketnew.com/";
#BeforeTest
public void beforeTest() {
System.out.println("Executing Before Test Block");
System.setProperty("webdriver.ie.driver", "c://IEDriverServer.exe");
WebDriver driver=new InternetExplorerDriver();
driver.get(baseurl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
#Test
public void test()
{
System.out.println("Executing Test Block");
System.out.println("The Page titile is "+ driver.getTitle());
}
}
#AfterTest
public void afterTest()
{
System.out.println("Executing After Test Block");
driver.close();
}
}
Kindly help me in resolving the above issue.
In your BeforeTest method, you are creating webdriver instance which has scope in beforeTest method only. It will throw null pointer in #Test and #AfterTest methods.
As you have already declared driver globally, so just initialize it in BeforeTest method like below:-
driver=new InternetExplorerDriver();
and not WebDriver driver=new InternetExplorerDriver();