AssertionViolatedException still occur using eclipse - java

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)

Related

What is the Java code for opening an incognito window and going to an URL?

I'm new to java. So this is the code i found to open a web page in default browser. I need it to open an incognito window in chrome and then go to the URL.
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
public class OpenWebPage {
public static void main(String[] args) {
try {
URI uri= new URI("https://www.google.com");
java.awt.Desktop.getDesktop().browse(uri);
System.out.println("Web page opened in browser");
} catch (Exception e) {
e.printStackTrace();
}
}
}
I think it can help.
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
See ChromeDriver from WebDriver and this.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Sel{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32\\chromedriver.exe");
// Initialize browser
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
WebDriver driver=new ChromeDriver(options);
// Open facebook
driver.get("https://www.facebook.com");
driver.manage().window().maximize();
}
}
This being the entire code. Thanks for the help!

java.lang.module.FindException: while using selenium

I am fairly new to selenium, and I was trying to use some of the scripts being used in tutorials for my practice. I downloaded all the required .JAR files (Chrome drivers, Selenium Java and Stand Alone server) and added it to the path in Eclipse.
Below is the Code which I am trying to run to access a Weblink and then trying to verify if a user is able to log in successfully or not.
package learnautomation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class practice {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "Mypath\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin#123");
driver.findElement(By.id("tdb1")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Successful");
}
else {
System.out.println("Unsuccessful");
}
driver.close();
}
}
While doing this I am getting this error:
"Error occurred during initialization of boot layer
java.lang.module.FindException: Module seleniumnew not found"
Also, import org.openqa.selenium.chrome.ChromeDriver; it says this is not accessible as well when I just hover over it.
try this, just edit paths and package name.
package navi;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Avi {
public static WebDriver driver;
WebDriverWait wait5s = new WebDriverWait(driver,5);
#BeforeClass
public static void setUpClass() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\pburgr\\Desktop\\chromedriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\Users\\pburgr\\AppData\\Local\\Google\\Chrome\\User Data");
driver = new ChromeDriver(options);
driver.manage().window().maximize();}
#Before
public void setUp() {}
#After
public void tearDown() {}
#AfterClass
public static void tearDownClass() {driver.close();driver.quit();}
#Test
public void avi() throws InterruptedException {
driver.get("http://www.gcrit.com/build3/admin/");
wait5s.until(ExpectedConditions.elementToBeClickable(By.name("username"))).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin#123");
driver.findElement(By.id("tdb1")).click();
// wait to resolve the click before checking url
Thread.sleep(1000);
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Successful");
}
else {
System.out.println("Unsuccessful");
}
}
}

ERROR:child_thread_impl.cc(762)] Request for unknown Channel-associated interface: ui::mojom::GpuMain

I try to use selenium(latest version, chrome version 58) with org.openqa.selenium.chrome.ChromeDriver. This class has documentation which appears in eclipse. It includes simple test:
import java.io.File;
import java.io.IOException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import junit.framework.TestCase;
#RunWith(JUnit4.class)
public class ChromeTest extends TestCase {
private static ChromeDriverService service;
private WebDriver driver;
#BeforeClass
public static void createAndStartService() {
service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"))
.usingAnyFreePort().build();
try {
service.start();
} catch (IOException e) {
System.out.println("service didn't start");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#AfterClass
public static void createAndStopService() {
service.stop();
}
#Before
public void createDriver() {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--disable-gpu");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
driver = new RemoteWebDriver(service.getUrl(), capabilities);
}
#After
public void quitDriver() {
driver.quit();
}
#Test
public void testGoogleSearch() {
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("webdriver");
// searchBox.quit();
assertEquals("webdriver - Google Search", driver.getTitle());
}
}
When I run it, new google chrome window appears on desktop. And in eclipse console I see:
ERROR:child_thread_impl.cc(762)] Request for unknown Channel-associated interface: ui::mojom::GpuMain
I tried to solve it by using :
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--disable-gpu");
But it didn't help. I still have the same error.
After I did debug I see that this error message appears on console when native method java.lang.Thread.start0() is called. Inside java.lang.Thread.start method there is call to native start0().
What is wrong with my comp(windows 7)?
How to solve this ?
Thank you!
Solved it.
I misunderstood method usingDriverExecutable of ChromeDriverService.Builder().
This method expects path to chromedriver.exe file but I set path to chrome.exe. Correct code is:
#BeforeClass
public static void createAndStartService() {
service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("D:\\Downloads\\chromedriver_win32\\chromedriver.exe"))
.withVerbose(false).usingAnyFreePort().build();
try {
service.start();
} catch (IOException e) {
System.out.println("service didn't start");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You can also set it with webdriver.chrome.driver system property:
System.setProperty("webdriver.chrome.driver", "D:\\Downloads\\chromedriver_win32\\chromedriver.exe");
To download latest chromedriver.exe go to https://sites.google.com/a/chromium.org/chromedriver/downloads. From this page they send you to https://chromedriver.storage.googleapis.com/index.html?path=2.29/.

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

Automated testing using selenium with firefox browser

I downloaded the code below and used it for some tests and u=it ran yesterday but since today the code stopped working. My tests are failing now which was not happening before. It throws up an error saying org.openqa.selenium.ElementNotVisibleException: element is not currently visible so cannot interact with element.
package org.openqa.selenium.example;
//import org.openqa.selenium.browserlaunchers.locators.GoogleChromeLocator;
//import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
//import org.openqa.selenium.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.chrome.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.Select;
//import org.openqa.selenium.net.UrlChecker;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class KongaUrlTest
{
private WebDriver driver;
private String baseUrl;
//private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception
{
driver = new FirefoxDriver();
baseUrl = "http://www.konga.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testFirefoxWebdriver() throws Exception
{
driver.get(baseUrl);
driver.findElement(By.cssSelector("a.vertnavlink > span")).click();
try
{
assertEquals("Phones & Tablets | Konga Nigeria", driver.getTitle());
System.out.println(driver.getTitle());
}
catch (Error e)
{
verificationErrors.append(e.toString());
}
}
#After
public void tearDown() throws Exception
{
System.out.println(driver.getCurrentUrl());
driver.quit();
}
}
There's a blocking dialog being displayed. It's probably displayed each time Selenium opens a new browser and navigates to that site. Close that dialog first:
driver.get(baseUrl);
try
{
driver.findElement(By.cssSelector(".fancybox-close")).click();
}
catch { }
driver.findElement(By.cssSelector("a.vertnavlink > span")).click();

Categories