ERROR: Element Not Found works well in Selenium IDE but not in Selenium RC. getting error - selenium-rc

this is my script. while clicking a link using its xpath not working & throws an error 'ERROR element not found' but works well in Selenium IDE.
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
public class testGoogle extends SeleneseTestCase {
#Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in/");
selenium.start();
selenium.windowFocus();
selenium.windowMaximize();
}
#Test
public void testUntitled() throws Exception {
selenium.open("http://www.google.co.in/");
selenium.waitForPageToLoad("30000");
assertEquals("Google", selenium.getTitle());
selenium.click("name=q");
selenium.type("name=q", "Software Testing");
selenium.click("name=btnK");
selenium.click("//html/body/div[4]/div/div/div[4]/div[3]/div[2]/div/div[2]/div/ol/li[2]/div/h3/a"); // ERROR: Element Not Found
selenium.waitForPageToLoad("30000");
}
#After
public void tearDown() throws Exception {
selenium.stop();
}
}

You can detect link as selenium.click("link=xyz") instead of xpath

The script is absolutely right. But the problem is that it is running very fast. When you start the script all the elements are not fully loaded. So add
selenium.setSpeed("1000");
as the very first line in function testUntitled(). Your script will run fine.
Cheers,
Amit Shakya

Just add "xpath=" before the path like this
xpath=/html/body/div[4]/div/div/div[4]/div[3]/div[2]/div/div[2]/div/ol/li[2]/div/h3/a
it will work faced this problem earlier..

I was also facing the issue - "Element not found" in Selenium RC.
Using selenium.setSpeed("1000") worked.

Related

load chromedriver in spring boot

I'm running a spring boot application and I'm trying to get the chromedriver not load from my local directory but rather from the project resources folder. I have my chromdriver.exe in resources/chromedriver.exe but I'm not sure how can I load it.
I tried this didn't work. Tried filepath to be "resources/chromedriver.exe" didn't work as well
String filePath = ClassLoader.getSystemClassLoader().getResource("resources/chromedriver.exe").getFile();
System.out.println(filePath);
System.setProperty("webdriver.chrome.driver", filePath)
If you are using Spring, you could try this:
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
Resource resource = new ClassPathResource("chromedriver.exe");
String filePath = resource.getFile().getPath();
System.out.println(filePath);
System.setProperty("webdriver.chrome.driver", filePath);
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.junit.Test;
public class GettingStarted {
#Test
public void testGoogleSearch() throws InterruptedException {
// Optional. If not specified, WebDriver searches the PATH for chromedriver.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com/");
Thread.sleep(5000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
Thread.sleep(5000); // Let the user actually see something!
driver.quit();
}
}
https://sites.google.com/a/chromium.org/chromedriver/getting-started
For example in Windows if chromedriver.exe is in C:/chromium use:
System.setProperty("webdriver.chrome.driver", "C:\\chromium\\chromedriver.exe");

Launch Firefox using Selenium

I am Trying to lunch Firefox and do a simple search on Google but i am only able to launch the Firefox. what should be edit in the code to run it smooth?I am Using Firefox Quantum 57 which runs using maven dependencies.
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirefoxLaunch
{
#Test
public static void main(String [] args) {
WebDriver driver;
driver = new FirefoxDriver();
driver.get("https://google.com");
WebElement element =driver.findElement(By.name("q"));
element.sendKeys("FirefoxDriver Search Function");
driver.quit();
}
}
While working with:
Selenium v3.x java clients
Firefox Quantum v70.x
You need to download the geckodriver binary from this link and save it in your system and then provide the absolute path of GeckoDriver binary through System.setProperty() line as follows :
System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe");

How to connect my Java applicationan to an Oracle database [duplicate]

This question already has answers here:
How to add JAR libraries to WAR project without facing java.lang.ClassNotFoundException? Classpath vs Build Path vs /WEB-INF/lib
(5 answers)
Closed 7 years ago.
The code below fails on the line:
Class.forName("oracle.jdbc.driver.OracleDriver");
with the error:
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
The two printlns print:
Wed_Jun_22_11:18:51_PDT_2005
false
This makes me think the class exists and can be found. Also this exact same class works in an a non-servlet application.
I have rebooted everything multiple times and regenerated the application/servlet multiple times. All values have been hard coded to make it simple and short.
private static Connection getDBConnection() throws Exception {
System.out.println(oracle.jdbc.driver.OracleDriver.BUILD_DATE);
System.out.println(Class.class.desiredAssertionStatus());
//load the driver
Class.forName("oracle.jdbc.driver.OracleDriver");
return DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:orcl", "SYSTEM", "pass");
}
full servlet that fails:
package servletClass_3;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class OneMoreBookStore
*/
#WebServlet("/OneMoreBookStore")
public class OneMoreBookStore extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
private static Connection getDBConnection() throws Exception {
System.out.println(oracle.jdbc.driver.OracleDriver.BUILD_DATE);
System.out.println(Class.class.desiredAssertionStatus());
//load the driver
Class.forName("oracle.jdbc.driver.OracleDriver");
return DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:orcl", "SYSTEM", "pass");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
Connection con = getDBConnection();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
This application works:
package servletClass_3;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnect {
private static Connection getDBConnection() throws Exception {
System.out.println(oracle.jdbc.driver.OracleDriver.BUILD_DATE);
System.out.println(Class.class.desiredAssertionStatus());
//load the driver
Class.forName("oracle.jdbc.driver.OracleDriver");
return DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:orcl", "SYSTEM", "pass");
}
public static void main(String[] args) {
try
{
Connection con = getDBConnection();
System.out.println("connection worked");
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
I'm using:
Eclipse JavaEE 1.4.2
Tomcat 7
jdk1.7
Oracle 11g R2
Windows 7 64bit
Probably you aren't deploying the oracle driver with your application.
You have several options:
You can place the driver jars in your WEB-INF/lib folder
You export it with your application. -> Right Click on Project -> Build Path-> Configure Build Path... -> Order and Export -> Check the drivers.
Place the driver jars in a shared or library extension folder of your application server. (You should go with option one or two though.)
You must include the ojdbc6.jar file in the Deployment Assembly of the Project...
select the web project which contains the jsp file...
select Project tab in the menu bar in Eclipse
select properties in the drop down menu
select Deployment Assembly
Add your ojdbc6.jar file in it.
Try this, change the oracle.jdbc.driver.OracleTypes to oracle.jdbc.OracleTypes

second scenario in my cucumber feature file is not executing

I'm trying to learn Cucumber with selenium java . have written two scenario's , when i run my feature file which contains two scenarios , only scenario #1 is executing , for scenario #2 its throwing Java null pointer exception
Feature: POC of my framework works
Scenario: Login test
Given I navigate to the Bugzilla website
When I click on login
And I enter the values
Then I check to see if i was successfully loged in or not
Scenario: File a bug test
Given I navigate to the File a bug page
When I click on widgets
And I enter the bug details
Then Bug should be submited succefully
My step definition file :
package cucumber.features;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class StepDefinitions1 {
protected WebDriver driver;
protected String baseUrl;
// Scenario 1
#Given("^I navigate to the Bugzilla website$")
public void I_navigate_to_the_Bugzilla_website() throws Throwable {
driver = new FirefoxDriver();
baseUrl ="https://landfill.bugzilla.org/bugzilla-4.4-branch/index.cgi";
driver.get(baseUrl);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#When("^I click on login$")
public void I_click_on_login() throws Throwable {
driver.findElement(By.id("login_link_top")).click();
}
#And("^I enter the values$")
public void I_enter_the_values() throws Throwable {
driver.findElement(By.id("Bugzilla_login_top")).clear();
driver.findElement(By.id("Bugzilla_login_top")).sendKeys("jeevan.anekal#gmail.com");
driver.findElement(By.id("Bugzilla_password_top")).clear();
driver.findElement(By.id("Bugzilla_password_top")).sendKeys("testuser#123");
driver.findElement(By.id("log_in_top")).click();
}
#Then("^I check to see if i was successfully loged in or not$")
public void I_check_to_see_if_i_was_successfully_loged_in_or_not() throws Throwable {
System.out.println("Login Successfull");
}
// Scenario 2
#Given("^I navigate to the File a bug page$")
public void I_navigate_to_the_File_a_bug_page() throws Throwable {
driver.findElement(By.id("enter_bug")).click();
}
#When("^I click on widgets$")
public void I_click_on_widgets() throws Throwable {
driver.findElement(By.linkText("Widgets")).click();
}
#And("^I enter the bug details$")
public void I_enter_the_bug_details() throws Throwable {
new Select(driver.findElement(By.id("bug_severity"))).selectByVisibleText("trivial");
new Select(driver.findElement(By.id("cf_drop_down"))).selectByVisibleText("---");
new Select(driver.findElement(By.id("rep_platform"))).selectByVisibleText("Macintosh");
new Select(driver.findElement(By.id("op_sys"))).selectByVisibleText("Mac OS X 10.0");
driver.findElement(By.id("short_desc")).clear();
driver.findElement(By.id("short_desc")).sendKeys("OS crashed");
driver.findElement(By.id("comment")).clear();
driver.findElement(By.id("comment")).sendKeys("Os debugging issue");
}
#Then("^Bug should be submited succefully$")
public void Bug_should_be_submited_succefully() throws Throwable {
System.out.println("Bug submitted successfully");
}
}
Looks like you are not calling the browser and navigating to the page in scenario 2
You need do do something similar to this
#Given("^I navigate to the File a bug page$")
public void I_navigate_to_the_File_a_bug_page() throws Throwable {
driver = new FirefoxDriver();
baseUrl ="https://landfill.bugzilla.org/bugzilla-4.4-nch/index.cgi";
driver.get(baseUrl);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("enter_bug")).click();
enter code here
}
or use #Before hooks to initialize the browser
I think I kind get your problem here. I had encountered similar problem before.
So when running the feature file, either by clicking on the run button from the bar or right click in the feature file then run..., take a look at what it says when actually clicking it. Because if you hover over a certain scenario and click run, it will only run that scenario, not the whole feature file where there is more than 1 scenarios.

Selenium server try to launch Safari browser for a Firefox test

I have a problem using a selenium server started with the selenium-maven-plugin. The server start normally with the command
mvn selenium:start-server
Then, I run the following test :
#Test
public void simpleTest() throws Exception {
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.firefox());
driver.get("http://localhost:8080/todolist-web/todo");
Assert.assertEquals("Todos", driver.getTitle());
driver.quit();
}
I get the following exception:
java.lang.RuntimeException: Safari could not be found in the path! Please add the directory containing ''Safari.exe'' to your PATH environment variable, or explicitly specify a path to Safari like this: safari c:\blah\Safari.exe`
It's true that Safari is not installed on my computer but, as you can see, I ran a Firefox test. So why is it looking for Safari browser ?
My pom.xml contains 2 jar :
- org.seleniumhq.selenium selenium-server 2.31.0
- org.seleniumhq.selenium selenium-firefox-driver 2.31.0
Please note it works correctly if I use the following code (added before my simpleTest()) instead of the maven command to launch the selenium server.
private static SeleniumServer server;
#BeforeClass
public static void setUpTest() throws Exception {
RemoteControlConfiguration conf = new RemoteControlConfiguration();
conf.setPort(4444);
conf.setDebugURL("/wd/hub");
server = new SeleniumServer(conf);
server.start();
}
#AfterClass
public static void tearDownTest() {
server.stop();
}

Resources