selenium - jUnit4 - selenium

I am trying to run the below code but in vain.
Code is not compiling and giving error as "selenium cannot be resolved".
Can anyone look into the below code -
package com.example.tests;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
public class prashantk {
#Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://book.theautomatedtester.co.uk/");
selenium.start();
}
#Test
public void testAuto_1() throws Exception {
selenium.open("/chapter2");
verifyEquals("Button with name", selenium.getValue("name=but2"));
verifyEquals("chocolate", selenium.getValue("xpath=(//input[#name='verifybutton'])[2]"));
selenium.click("link=Index");
selenium.waitForPageToLoad("60000");
verifyTrue(selenium.isTextPresent("Chapter4"));
}
#After
public void tearDown() throws Exception {
selenium.stop();
}
}

You have references to a field named selenium, but there is no such field defined.
Somewhere in your class, probably on a line just before the #Before, you want to add this field:
Selenium selenium;
Apparently from comments you also don't have the methods verifyEquals and verifyTrue referenced in your code. Those methods are defined in a base class SeleneseTestCase which your test should extend:
public class prashantk extends SeleneseTestCase {

Replace the below line in your code as shown below
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://book.theautomatedtester.co.uk/");
to
DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://book.theautomatedtester.co.uk/");

Looking at code gives an impression it was done in Selenium IDE first the exported to a tool like eclipse.
I really don't think 'verifyEquals' exist in WebDriver instead use JUnit assertions or Hamcrest assertions. Try to write your code from scratch in Java its a lot less hassle.

Related

Exception in thread Unresolved compilation problems:

This is my script:
package sampleTests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AdminLogin {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver(); //Launches Firefox Browser with blank url
driver.close();
}
}
When I run the script, am getting below error, I tried to add the java lib in Class path instead of Module path, still the issue not resolved, someone please help;
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 sampleTests.AdminLogin.main(AdminLogin.java:10)
You have to set the path of the Geckodriver (the executable that is used for Firefox tests using Selenium).
Search for Geckodriver in Google.
Download the executable as per your system (Win/Mac/Linux)
Set the path of the executable in your tests as given below -
package sampleTests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AdminLogin {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver",Path_of_Firefox_Driver");
// example for Windows
System.setProperty("webdriver.gecko.driver",D:\\GeckoDriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://testautomationu.applitools.com/");
driver.quit();
}
}

TestNG error via command line : Cannot instantiate class testCases.LoginPage

Cloned the project from github at location :
C:\Automation\CC_Regression_Automation\CC_Regression
When trying to run testng.xml using eclipse all goes well.
Getting Cannot instantiate class testCases.LoginPage when trying to run the code using command line.
**Loginpage.java**
package testCases;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.relevantcodes.extentreports.ExtentTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import utils.DriverUtil;
import utils.Loggers;
import utils.ReportGenerator;
import utils.WebPageUtils;
public class LoginPage extends Base{
/*Members of the current Test Class. The number varies from script
to script depending on the variables and verifications required*/
public WebDriver driver;
private String currentSitePath;
private String testCaseName=getClass().getName().substring(10);
ExtentTest parentTest =ReortGenerator.initializeParentTest(getClass().getName().substring(10),"Testing Login Page");
//Function to Navigate to a particular URL inside CC
public void navigateToURL(WebDriver driver){
siteURL="";
this.driver.navigate().to(baseurl+siteURL);
}
#Test // Main Test Flow for the Script
public void executeScript() throws IOException{
System.out.println("*******************");
System.out.println("launching chrome browser");
//Test Case Author assignment in Reports
ReportGenerator.assignAuthor(parentTest,"Garima");
//Setting up Browser Instance
this.driver=driverIns();
//Navigating to the required page in CC
navigateToURL(this.driver);
Sleep(5000);
String strPageTitle = this.driver.getTitle();
System.out.println(strPageTitle);
//Start Logs
Loggers.startCurrentTestCaseExecution(this.driver);
try{
ReportGenerator.verifyNavigation(this.driver, "Control Center", parentTest,testCaseName,"Yes");
Code snippent of Base.java
//Function to instantiate the WebDriver instance based on the Browser
selected for Windows
public WebDriver driverInsWindows(){
isExtensionEnabled();
setbaseURL();
try {
switch(getBrowser()+isExtensionEnabled.toString()){
case "Chromefalse":
System.setProperty("webdriver.chrome.driver", "./Win/Drivers/chromedriver.exe");
driver=new ChromeDriver();
driver.get(baseurl);
break;
case "Chrometrue":
System.setProperty("webdriver.chrome.driver", "./Win/Drivers/chromedriver.exe");
driver=invokeChromeBrowserwithExtension();
driver.get("https://www.google.com");
break;
case "Internet Explorer":
System.setProperty("webdriver.internetexplorer.driver",
"./Win/Drivers/internetexplorerdriver.exe");
driver=new InternetExplorerDriver();
driver.get(baseurl);
break;
case "Firefox":
driver=new FirefoxDriver();
driver.get(baseurl);
break;
default:
//new PascalBaseClass();
}
} catch (IOException e) {
}
Executing below command via cmd:-
java -cp C:\Automation\CC_Regression_Automation\CC_Regression\bin;C:\Automation\CC_Regression_Automation\CC_Regression\lib\* org.testng.TestNG testng.xml
Blockquote
Your problem might be this line:
ExtentTest parentTest =ReortGenerator.initializeParentTest(getClass().getName().substring(10),"Testing Login Page")
Since I'm seeing other references to ReportGenerator
I doubt you typed all that code, and it's a copy/paste so I'm not sure why your IDE doesn't flag it, or is there somehow another object called ReortGenerator

Selenium webdriver running issue

I am trying to running selenium webdriver using eclipse.Unfortunately i face the following issue:
click here for the snapshot
and the code is,
package myproject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MyClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "K:\\New folder\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver(); driver.get("http://only-testing-blog.blogspot.in");
String i = driver.getCurrentUrl();
System.out.println(i); driver.close();
}
}
UPDATE:
If you right click on your project and "Run as... Java application" and you have multiple main methods in your project, eclipse doesn't know which one to start so it asks which class. The solution is to right click on your class and "Run as...". After that you can just press "Run".

selenium firefox path setting problems

no matter what I do selenium doesn't accept my firefox path in the console where I launch it from. Keep getting the same error.
I also set the path as an environment variable in win 7 64.
I'm pretty much a virgin when it comes to selenium. Trying to run my first JUNIT test.
Here is the message from selenium.
21:46:43.481 INFO - Command request: getNewBrowserSession[*chrome, http://compendiumdev.co.uk/, ] on session null
21:46:43.497 INFO - creating new remote session
21:46:43.544 INFO - Got result: Failed to start new browser session: java.lang.RuntimeException: java.lang.RuntimeException: Firefox could not be found in the path!
Please add the directory containing ''firefox.exe'' to your PATH environment
variable, or explicitly specify a path to Firefox like this:
*firefox c:\blah\firefox.exe on session null
Below is my java code
package com.eviltester.seleniumtutorials;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class MyFirstSeleniumTests {
Selenium selenium=null;
#Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome",
http://compendiumdev.co.uk/");
selenium.start();
}
#Test
public void testExported() throws Exception {
selenium.open("/selenium/search.php");
selenium.type("q", "Selenium-RC");
selenium.click("name=btnG");
}
#After
public void tearDown() throws Exception {
selenium.stop();
}
}
Thank you for the help.

how call external class (contains selenium commands) in selenium test properly?

I use Selenium Web Driver with JUnit in Eclipse. I want diminish my code by creating new classes for repitable steps. I want store this classes in separate files for convenience. For example this one of such classes:
import org.openqa.selenium.ie.InternetExplorerDriver;
//Login
public class Login {
private InternetExplorerDriver driver;
String url;
String name;
String password;
String language;
public Login (String ur, String nam, String pass, String lang){
url=ur;
name=nam;
password=pass;
language=lang;
}
public void log_in (){
driver.get(url);
driver.findElement(By.name("username")).sendKeys(name);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.name("language")).sendKeys(language);
driver.findElement(By.name("logon_action")).click();
}
}
This is my main test:
package bobsworld;
import init.Login;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class WebTest {
private WebDriver driver;
#Test
public void testUnit() throws Exception {
//Open new window
System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
driver = new InternetExplorerDriver();
//Login
init.Login login = new init.Login ("myurl", "log","pas","English");
login.log_in();
}
The problem is with object driver. I get java.lang.NullPointerException and can't execute test. How should I organize call to Login and code to make my test work?
What you try to achieve is a quite popular approach called "PageObjects".
Your issues:
Use some checkstyle tool to improve code quality
Do a whole lot of research about Java in general
The Login Class or "init.Login" (it's quite unusual to have a part of the package as prefix for the class) is not able to use the same WebDriver instance because you don't forward it. Normally you would have opened a second WebDriver instance but that seems to be impossible though I don't know your setup. You have to forward the "driver" as a parameter to the constructor of the Login class.
Try to use real PageObjects as described here inluding PageFactory to improve readability and maintainability