FireFox Driver not working with Selenium - java

WHAT I HAVE DONE SO FAR:
I have included selenium as dependency in build.gradle. It looks like
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.3.1'
}
And in my test class I have included a small code as below.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestClass {
#Test
public void Test1(){
System.setProperty("webdriver.gecko.driver", "/Users/xyz/Downloads/geckodriver");
DesiredCapabilities capabilities=DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("http://www.google.com");
driver.close();
driver.quit();
}
}
And I have created a Testng instance to run the test case in intellij.
PROBLEM I AM FACING
After I execute the test. I get the following error.
org.openqa.selenium.WebDriverException: failed to lookup address information: nodename nor servname provided, or not known
I am not sure how should i go about debugging it.

Check path when you setProperty
System.setProperty("webdriver.gecko.driver",
"D:\workspacess\src\config\geckodriver.exe"); WebDriver driver
= new FirefoxDriver();

Related

Seems adding browsermob-core to my dependencies causes my selenium webdriver to stop working

Here is the code. It's just an example test stuff works without browermob-core. With core I get NoSuchMethodError
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
import java.util.Objects;
public class ChromeScriptTest {
public WebDriver driver;
#Test
public void eightComponents() {
System.setProperty("webdriver.chrome.driver", Objects.requireNonNull(getClass().getClassLoader().getResource("drivers/chromedriver.exe")).getFile() );
driver = new ChromeDriver();
driver.get("https://google.com");
Assertions.assertEquals("Google", driver.getTitle());
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
WebElement searchBox = driver.findElement(By.name("q"));
WebElement searchButton = driver.findElement(By.name("btnK"));
searchBox.sendKeys("Selenium");
searchButton.click();
searchBox = driver.findElement(By.name("q"));
Assertions.assertEquals("Selenium", searchBox.getAttribute("value"));
driver.quit();
}
}
Gradle Dependencies - I've tried changing the order and stuff still has the same error.
dependencies {
implementation 'junit:junit:4.13.2'
implementation 'org.junit.jupiter:junit-jupiter:5.8.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
implementation group: 'net.lightbody.bmp', name: 'browsermob-core' , version: '2.1.5'
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.1.0'
implementation group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '5.0.3'
}
And the error. when I remove the browermob-core everything works fine
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
'io.netty.bootstrap.AbstractBootstrap io.netty.bootstrap.Bootstrap.channelFactory(io.netty.channel.ChannelFactory)'
java.lang.NoSuchMethodError: 'io.netty.bootstrap.AbstractBootstrap io.netty.bootstrap.Bootstrap.channelFactory(io.netty.channel.ChannelFactory)'
at org.asynchttpclient.netty.channel.ChannelManager.newBootstrap(ChannelManager.java:162)
at org.asynchttpclient.netty.channel.ChannelManager.<init>(ChannelManager.java:149)
at org.asynchttpclient.DefaultAsyncHttpClient.<init>(DefaultAsyncHttpClient.java:92)
at org.asynchttpclient.Dsl.asyncHttpClient(Dsl.java:32)
Searched a while and found the answer from this How solve Error: java.lang.ClassNotFoundException: io.netty.util.concurrent.GenericFutureListener?
Seems either I had some cross-path issue or missing/dated netty dependent
added this to my Gradle build
implementation group: 'io.netty', name: 'netty-all', version: '4.1.75.Final'

How to use Jailbreak from java manifold library correctly for junit testing?

I am using java's manifold extension library for junit testing and i cant figure what i am doing wrong even after following exactly their docs.
// My Class
package practice_junit;
public class SomeClass
{
public SomeClass()
{
}
private String get_string()
{
return "ABCDE";
}
}
// My Unit Test Class -- first way
package practice_junit;
import manifold.ext.api.Jailbreak;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class SomeClassTest
{
public SomeClassTest()
{
}
#Test
public void assert_equals_true_test()
{
#Jailbreak SomeClass sc = new SomeClass();
assertEquals("Error equals","ABCDE",sc.get_string());
}
}
// My Unit Test Class -- second way
package practice_junit;
import manifold.ext.api.Jailbreak;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class SomeClassTest
{
public SomeClassTest()
{
}
#Test
public void assert_equals_true_test()
{
SomeClass sc = new SomeClass();
assertEquals("Error equals","ABCDE",sc.jailbreak().get_string());
}
}
In both the cases i am getting same error log :-
PS C:\Users\> gradle build
> Task :compileTestJava FAILED
C:\Users\SomeClassTest.java:19: error: get_string() has private access in SomeClass
assertEquals("Error equals","ABCDE",sc.get_string());
^
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileTestJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1m 9s
3 actionable tasks: 1 executed, 2 up-to-date
I am using gradle and manifold extension dependency as compile group: 'systems.manifold', name: 'manifold-ext', version: '2019.1.12' from https://mvnrepository.com/artifact/systems.manifold/manifold-ext/2019.1.12
What version of Java are you using? If Java 9 or later, are you using the JPMS (modules)? If you post your Gradle script, I can help you set it up properly. Better, post an issue on the manifold github with a link to your project. It may be that the --module-path is not explicitly set, which is a very common problem with Gradle scripts using Java 9+. Here's are the relevant bits:
dependencies {
compile group: 'systems.manifold', name: 'manifold-ext', version: '2019.1.12'
testCompile group: 'junit', name: 'junit', version: '4.12'
// Add manifold to -processorpath for javac (for Java 9+)
annotationProcessor group: 'systems.manifold', name: 'manifold-ext', version: '2019.1.12'
}
compileJava {
doFirst() {
// If you DO NOT define a module-info.java file:
options.compilerArgs += ['-Xplugin:Manifold']
// if you DO define a module-info.java file:
//options.compilerArgs += ['-Xplugin:Manifold', '--module-path', classpath.asPath]
//classpath = files()
}
}
The Manifold project tends to use Maven everywhere; the Gradle setup docs are not as polished.

IllegalStateException when using Mockito with IntelliJ's Java

The error message I receive:
java.lang.IllegalStateException: Could not initialize plugin:
interface org.mockito.plugins.MockMaker
The code in question:
List<String> mockList = mock(List.class);
The build.gradle dependency:
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
def mockito_version = 'latest.release'
// For local unit tests on your development machine
testCompile "org.mockito:mockito-core:$mockito_version"
}
I have tried looking at other people with the same issue and I keep getting references to PowerMock. I have no idea what that means, so if this is a duplicate I apologize. It just seems like no other question had a solution that resolved my issue. The library is imported properly, as I do not have any compilation errors. Any help would be greatly appreciated.
i tried to create a new project and test this out. below is how my depdencies in gradle file looks:
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile "org.mockito:mockito-core:2.+"
}
below is my test class:
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class TestList {
#Test
public void Test(){
List<String> myList = mock(List.class);
when(myList.get(0)).thenReturn("hello world");
Assert.assertEquals("hello world",myList.get(0));
}
}
this works.

Selenium 3 InternetExplorerDriver Setup

I'm trying to set up a type of boiler-plate code for WebDriver that I can give to anyone in my QA team to help them test. My problem is that I can't seem to get Internet Explorer working. It's throwing errors and I have no idea how to fix them or if its some sort of naming issue.
The driver files are all in my C:\ Drive.
chromedriver.exe, geckodriver.exe, IEDriverServer.exe
Errors in the code below are //commented
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.IEDriverService; //The import org.openqa.selenium.ie.IEDriverService cannot be resolved
public class Loginmethod {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:\\\\geckodriver.exe");
System.setProperty("webdriver.chrome.driver", "C:\\\\chromedriver.exe");
System.setProperty("webdriver.ie.driver", "C:\\\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(); //InternetExplorerDriver cannot be resolved to a type
driver.get("http://www.google.com/");
Thread.sleep(100);
}
}
Additionally, If anyone would know a way to test for Safari using windows 10 with selenium, that would be great.
The class you are trying to import is not the class you are using.
You are importing IEDriverService but using the InternetExplorerDriver class.
change your code to import InternetExplorerDriver.

Unit testing Cloud Datastore with Android Studio

I am trying to write unit tests for a Cloud Endpoints backend, specifically using Datastore. (Unit testing without access to any specific Android modules is working as expected.)
Per this question, I added the following to my backend's build.gradle:
testCompile 'com.google.appengine:appengine-api-labs:1.9.8'
testCompile 'com.google.appengine:appengine-api-stubs:1.9.8'
testCompile 'com.google.appengine:appengine-testing:1.9.8'
testCompile 'junit:junit:4.12+'
My code is minimized from this tutorial and located at backend/src/test/java/<package>:
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import org.junit.Test;
import static junit.framework.Assert.assertTrue;
public class ExampleDatastoreTest {
private final LocalServiceTestHelper datastoreHelper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
#org.junit.Before
public void setUp() throws Exception {
datastoreHelper.setUp();
}
#org.junit.After
public void tearDown() throws Exception {
datastoreHelper.tearDown();
}
#Test
public void testExample() {
assertTrue(true);
}
}
I am getting NoClassDefFoundError when I call tearDown on my LocalServiceTestHelper.
Per that tutorial, I still need to include:
${SDK_ROOT}/lib/impl/appengine-api.jar
${SDK_ROOT}/lib/impl/appengine-tools-sdk.jar
Using File > Project Structure in Android Studio 1.2.2, I found these options:
testCompile 'com.google.appengine:appengine-tools-sdk:1.9.25'
testCompile 'com.google.appengine:appengine-api-1.0-sdk:1.9.25'
testCompile 'com.google.appengine:appengine:1.9.25'
The appengine-tools-sdk looks good, but I don't seem to be able to find appengine-api. The two I listed above are the closest I found. I continue to get the same error.
What am I missing here?

Categories