Using Jdk 10 and Oxygen 3a - java

In the following program the 2nd and 3rd import statements are erroring out by the following statement - The package org.openqa.selenium is accessible from more than one module: client.combined, com.google.common.
The following are the jar files I added to module path.
Program with errors
After compilation
This program was running successfully with jdk 9 and oxygen. But its is failing now. Please let me know what corrections I can do to use the webdriver in my project.
Thanks in Advance.
package javaExamples;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public class FirstTrial {
WebDriver driver;
JavascriptExecutor jse;
public void invokeBrowser() {
try {
System.setProperty("webdriver.chrome.driver",
"C:\\Selenium\\chromedriver_win32_2018_april\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);// allows system to wait max 30 secs if it
// completes before its fine
driver.get("http://www.edureka.co");
searchBrowser();
} catch (Exception e) {
e.printStackTrace();
}
}
public void searchBrowser() {
driver.findElement(By.id("homeSearchBar")).sendKeys("java");
// Thread.sleep(2000);//allows to wait for 2 secs. This is
explicit wait. System // must sleep for 2 secs
driver.findElement(By.id("homeSearchBarIcon")).click();
jse = (JavascriptExecutor) driver;
jse.executeScript("scroll(0,600)");
driver.findElement(By.xpath("//label[contains(text(),'Weekend')]")).click();
}
public static void main(String[] args) {
FirstTrial myobj = new FirstTrial();
myobj.invokeBrowser();
}
}

Related

Multiple login sessions not getting closed with driver.close in java-selenium

I have added code snippet for further clarity.
I am using java-selenium-testNG and trying to login to a website zoho.com with 3 accounts and verifying if success. I have looked through somewhat similar issues but haven't found a solution that works in my case.
I instantiate the ChromeDriver in #BeforeMethod. Then I login to the website with first account and close the browser in my #AfterMethod.
I have to re-instantiate the browser with Driver = new ChromeDriver to login back with a new account as the instance is closed or atleast that's the feeling it gives error that the session id doesn't exists.
My issue is only the last instance of the driver is getting closed as I have driver.quit in the #AfterTest method. The other two instances remain in memory.
I have also tried using the same instance without closing the browser but in such cases the login option is not available on that particular website and my subsequent tests fail.
here is the code below for further clarification
package uk.dev.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.util.concurrent.TimeUnit;
public class zohoLogin {
WebDriver driver=null;
String winHandleBefore;
#DataProvider(name ="testData")
public Object[][] loginPasswordData()
{
return new Object[][]{
{"xxx#gmail.com",new String ("somepassword")} ,
{"yyy#gmail.com",new String ("somepassword")},
{"zzz#gmail.com",new String ("somepassword")},
};
}
#Test ( dataProvider = "testData")
public void enterUserDetails(String userEmail, String userPassword) throws InterruptedException {
driver.findElement(By.xpath("//input[#id='login_id']")).sendKeys(userEmail);
System.out.println(("Running for Test data " + userEmail));
Thread.sleep(2000);
driver.findElement(By.xpath("//button[#id='nextbtn']")).click();
Thread.sleep(2000);
System.out.println("clicked on Next");
driver.findElement(By.xpath("//input[#id='password']")).sendKeys(userPassword);
System.out.println("Entered the password");
driver.findElement(By.xpath("//button[#id='nextbtn']//span[contains(text(),'Sign in')]")).click();
System.out.println("Clicked on Signin");
Thread.sleep(2000);
}
#BeforeMethod
public void loginZoho() throws InterruptedException
{
driver = new ChromeDriver();
driver.get("https://www.zoho.com");
System.out.println("Open the browser");
driver.findElement(By.xpath("//a[#class='zh-login']")).click();
//driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
Thread.sleep(5000);
}
#AfterMethod
public void closeBrosers() throws InterruptedException {
Thread.sleep(2000);
driver.close();
System.out.println("Close the browser");
}
#BeforeTest
public void setupBrowser()
{ WebDriverManager.chromedriver().setup();
}
#AfterTest
public void tearDown()
{
driver.quit();
}
}
Attached the run results where 2 chrome driver instance can be seen.Also note the AfterMethod is not getting executed.
enter image description here
I am using driver.quit() method in selenium but facing the same issue. I think issue is related to the latest version of chromedriver.
public static void CloseDriver()
{
if (Driver != null)
{
Driver.Quit();
Driver = null;
}
}
I even tried Driver.Kill() but it resulted in closing all the browsers.

Thread.sleep not running [duplicate]

This question already has answers here:
How do I make a delay in Java?
(8 answers)
Closed 4 years ago.
I have some code with the following structure in Eclipse:
package automationFramework;
import java.util.List;
import org.openqa.selenium.support.ui.Select;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirefoxDropDown {
public static void main(String[] args) throws InterruptedException {
// Create a new instance of the Firefox driver
System.setProperty("webdriver.gecko.driver", "/home/gradulescu/Documents/Eclipse project/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Storing the Application URL in the String variable
String url= "http://toolsqa.wpengine.com/automation-practice-form/";
driver.get(url);
//Launch the Online Store Website
Select oSdropDown = new Select((WebElement)driver.findElement(By.id("continents")));
oSdropDown.selectByIndex(1);
Thread.sleep(100);
oSdropDown.selectByVisibleText("Africa");
Thread.sleep(100);
List<WebElement> oSize = oSdropDown.getOptions();
int size = oSize.size();
for(int i=0;i<size;i++)
{
String sValue = oSdropDown.getOptions().get(i).getText();
System.out.println(sValue);
}
driver.quit();
}
}
My expectation would be that after the first code ran, 10 seconds to be waited and then the second code and some other 10 seconds. But actually the compiler runs command after command without waiting the 10 seconds I have set.
Is there any mandatory condition for it to work?
Thank you!
I don't think you're waiting long enough, try: Thread.sleep(10000);
You can also use: Thread.sleep(TimeUnit.SECONDS.toMillis(10));

AssertionViolatedException still occur using eclipse

I tried running this code in my laptop. I followed all the instruction showed in Edureka in youtube. Tried uninstall and reinstalling method. I transferred files from a laptop where the code work and yet, still no good.
package testing;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import net.bytebuddy.asm.Advice.Enter;
public class test_1 {
WebDriver driver;
public void invokeBrowser() {
try {
System.setProperty("webdriver.chrome.driver", "D:\\Program Files (x86)\\Files\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.get("http://google.com/");
LogIn();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
test_1 myObj = new test_1();
myObj.invokeBrowser();
}
}
then here's the error
Exception in thread "main"
org.apache.bcel.verifier.exc.AssertionViolatedException:
FOUND:
INTERNAL ERROR: Oops!
Exiting!!
at
org.apache.bcel.verifier.exc.AssertionViolatedException.main(AssertionViolatedException.java:102)

ChromeDriver cannot navigate to URL in eclipse

I am very new to Cucumber and get the following error using ChromeDriver to request a URL:
java.lang.IllegalStateException: The path to the driver executable
must be set by the webdriver.chrome.driver system property; for more
information, see http://code.google.com/p/selenium/wiki/ChromeDriver.
The latest version can be downloaded from
http://chromedriver.storage.googleapis.com/index.html at
com.google.common.base.Preconditions.checkState(Preconditions.java:177)
My code:
package cucumber.features;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class AddToList {
WebDriver driver = null;
#Given("^I am on Todo site$")
public void onSite() throws Throwable {
driver = new ChromeDriver();
driver.navigate().to("http://localhost");
System.out.println("on todo site");
}
#When("^Enter a task in todo textbox$")
public void enterTask() throws Throwable {
driver = new ChromeDriver();
driver.findElement(By.name("task")).sendKeys("Test Unit Using Cucumber");
;
System.out.println("task entered");
}
#Then("^I click on add to todo$")
public void clickAddToTodo() throws Throwable {
driver = new ChromeDriver();
driver.findElement(By.xpath("//input[#value='Add to Todo' and #type='button']"));
System.out.println("add button clicked");
}
}
I had a similar problem when using selenium library. I found this line before creating my driver fixed it.
System.setProperty("webdriver.chrome.driver", PATH_TO_CHROME_DRIVER);
Here is simple project that could help you.
this.driver.get(URL);
Also, I don't think your When and Then should create new ChromeDrivers. Only the Given. I use the setUp method to instantiate it
#Before
public void setUp() {
System.setProperty("webdriver.chrome.driver", "..//..//files//drivers//chromedriver.exe");
this.driver = new ChromeDriver();
}

Running Selenium tests with Chrome on Ubuntu in a headless environment

There's a massive amount of postings on this topic, but for some reason, I've found little relating to Chrome (it seems the support was recently added for headless). The tests work running locally with a browser.
Is it possible to trigger an Xvfb display from Java so I can manage everything in one place? I'd rather not have to do:
Xvfb :1 -screen 0 1024x768x24 &
Via the command line. It'd be useful to run a display, then shut it down.
SetEnvironmentProperty to ChromeDriver programatically is what I found to be the solution.
The problem? I can't get it to work on an Ubuntu machine.
The error I receive is:
/home/ubuntu/myproject/conf/chromedriver: 1: Syntax error: Unterminated quoted string
The test class I'm using:
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import play.Logger;
import utils.LocalInfo;
import com.google.common.collect.ImmutableMap;
public class TestCI {
private WebDriver driver;
#SuppressWarnings("deprecation")
public TestCI(String url) {
if (!LocalInfo.isEC2()) {
Logger.info("Running tests by opening a browser...");
System.setProperty("webdriver.chrome.driver", "conf/chromedriver");
setWebDriver(new ChromeDriver());
} else {
Logger.info("Running tests headlessly...");
String xPort = System.getProperty("Importal.xvfb.id", ":1");
ChromeDriverService service = new ChromeDriverService.Builder()
.usingChromeDriverExecutable(new File("conf/chromedriver"))
.usingAnyFreePort()
.withEnvironment(ImmutableMap.of("DISPLAY", xPort)).build();
setWebDriver(new ChromeDriver(service));
}
getWebDriver().get("http://www.google.com");
try {
someUITest();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
getWebDriver().close();
}
public WebDriver getWebDriver() {
return driver;
}
public void setWebDriver(ChromeDriver driver) {
this.driver = driver;
}
public void someUITest() throws Exception {
getWebDriver().findElement(By.name("q"));
}
}
I switched to the chromedriver for linux and it got rid of the unterminated quoted string error http://code.google.com/p/chromedriver/downloads/list

Categories