I'm new to selenium and still exploring it. I'm trying to test an ajax element on a site. On clicking the element some text pops on some area on the page. I've run this test on RC as well as WebDriver. It's working fine. Now I want(just out of curiosity) to test it via WebDriverBackedSelenium. But it's throwing an error. The code is:
import com.thoughtworks.selenium.SeleneseTestBase;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.internal.WrapsDriver;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class CheckElements extends SeleneseTestBase {
#Before
public void setUp() throws Exception {
String baseUrl = "path/to/the/site";
WebDriver driver = ((WrapsDriver) selenium).getWrappedDriver();
selenium = new WebDriverBackedSelenium(driver, baseUrl);
}
The error is:
java.lang.NullPointerException
at com.CheckElements.setUp(CheckElements.java:17)
I believe the main problem is in setup method. I'm using selenium standalone server 2.0 and junit 4.7. Can you please tell where am I going wrong?
Another Question??
If I write the same test as below, the test runs fine but it doesn't give error even if the text doesn't matches what is required.
import com.thoughtworks.selenium.SeleneseTestBase;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class New extends SeleneseTestBase {
WebDriver driver;
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
String baseUrl = "path/to/the/link";
selenium = new WebDriverBackedSelenium(driver, baseUrl);
}
I hope I'm clear in my questions. Thanks.
I would highly expect this to be the issue:
WebDriver driver = ((WrapsDriver) selenium).getWrappedDriver();
The driver instance is probably null.
The difference being in your other test, you are explicitly creating it into a FirefoxDriver:
driver = new FirefoxDriver();
Related
I am new to Selenium webdriver, and trying to test a webpage which has some dynamic links (dynamic ads). Example : ads on https://mail.rediff.com/cgi-bin/login.cgi
I tried with xpath ,classname and id but none of these is working.It is happening because every time new content is displaying on the page so it is not able to find the element and throwing Exception in thread "main":
org.openqa.selenium.NoSuchElementException: Unable to locate element: #map.
my code is :
package Selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Image_Link {
static WebDriver driver ;
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//System.setProperty("webdriver.chrome.driver", "E://chromedriver_win32//chromedriver.exe");
//driver = new ChromeDriver();
System.setProperty("webdriver.gecko.driver", "E://geckodriver-v0.21.0-win64//geckodriver.exe");
driver = new FirefoxDriver();
Actions actions = new Actions(driver);
driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
//WebElement Dynamic_ads=driver.findElement(By.className("rhs-area floatR"));
WebElement Dynamic_ads=driver.findElement(By.id("map"));
actions.moveToElement(Dynamic_ads).perform();
WebElement ad_Link = driver.findElement(By.cssSelector("#map > area:nth-child(2)"));
actions.moveToElement(ad_Link);
actions.click();
actions.perform();
//driver.navigate().to("www.google.com");
//String value = driver.findElement(By.id("hplogo")).getAttribute("title");
}
}
The elements for the dynamic ads cannot be found because it isn't fully loaded yet on load page. I recommend adding an explicit wait time for the specific add you're looking for. You can check out this link here for more information.
Anyhow, here's your solution:
1.)Implement WebDriverWait: WebDriverWait wait = new WebDriverWait(driver, 20);
2.) Change your Dynamic_ads data to wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("map"))); then do Dynamic_ads.click(); afterwards.
OR
2.) Change your Dynamic_ads data to wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//map[#id='map']/*"))); then do Dynamic_ads.click(); afterwards. NOTE: This will pick the first child node.
I am getting some sort of compilation error it seems.
I have ‘Common.java’ class inside ‘base’ package. It's a program for starting firefox driver. That, I have kept it in the separate package to make it a one time effort and modularity purpose.
I am calling this class file inside the child class ‘tc_01.java’. This TC_01.jave program is in another package ‘testing’. This TC_01.java file is actually accessing driver from Common.java and start the browser and try some login and logout actions.
My child class TC_01.java is showing me compilation error and Error Message on Mouse Hover is – > “field Common.driver is not visible”.
And, at Console : "java.lang.Error: Unresolved compilation problems:
The field Common.driver is not visible"
My Analysis: It seems TC_01.java file is not able to access the 'driver' from 'Common.java'.
But, I have already written 'extends' keyword for it to inherit the properties.
Please guide me. Thanks
Here is my code:->
package base;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
public class Common {
public FirefoxDriver driver;
#BeforeMethod
public void BM(){
System.setProperty(“webdriver.gecko.driver”,”D://Rajiv//Selenium//geckodriver.exe”);
driver = new FirefoxDriver();
driver.get(“http://automationpractice.com/index.php”);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#AfterMethod
public void CM(){
driver.close();
}
}
# Pakage – testing; Class – Tc_01.java
package testing;
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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import base.Common;
public class TC_01 extends Common{
public FirefoxDriver driver;`
#Test
public void TM(){
System.out.println(“Selenium Webdriver Script in Firefox browser using Gecko` `Driver | AutomationPractice.com PAGE LAUNCHED”);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“search_query_top”)));
try{
String expectedTitle = “My Store”;
System.out.println(“ExpectedTile = “+expectedTitle );
String actualTitle = driver.getTitle();
System.out.println(“The actual Title of the Page is = “+actualTitle);
Assert.assertEquals(actualTitle, expectedTitle);*/
System.out.println(“Control has reached here”);
driver.findElementByClassName(“login”).click(); // field common.driver is not visible
driver.findElementById(“email”).sendKeys(“*****#yahoo.com”);
driver.findElementById(“passwd”).sendKeys(“*****”);
driver.findElementById(“SubmitLogin”).click();
driver.findElementByClassName(“logout”).click();
System.out.println(“Sucessfully Logout from the Application”);
}catch (Exception e){
e.printStackTrace();
}
}
}
I was able to find the root cause for this issue.
It wasted my 3 days before i asked for help here in this forum.
As i had analysed initially, i had mentioned my driver properties in parent class and was accessing it in Child Class. There was some name mismatch. When it was trying to inherit the properties from parent, it was not accessible and giving me compilation error. I renamed the parent class name to the same name that i was giving while extending in Child Class. And, it worked.
Thanks again to all of you. It is such a wonderful forum to discuss your issue between each other and getting its resolution.
I am beginner for Selenium, just I want to check that search of the website working properly. Means when I enter any keyword in the search box and click on search is it provides correct search result. How to Check using Selenium WebDriver. Please guide me.
You should provide more details about your query
for example you should include the URL your are working on or a similar URL for the one you are working on
You can use an item property from the search result page as an assertion point for your test
Try below code
package StackOverFlow.StackOverFlow;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class App
{
#Test
public void test () throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "C:\\Chrome\\2\\chromedriver.exe");
WebDriver Dr = new ChromeDriver();
Dr.manage().window().maximize();
Dr.get("http://www.conns.com");
Thread.sleep(4000);
WebElement ele = Dr.findElement(By.xpath(".//*[#id='search']"));
ele.sendKeys("furniture");
ele.sendKeys(Keys.ENTER);
Thread.sleep(4000);
String result= Dr.findElement(By.xpath("/html/body/div[3]/div/div[4]/div[2]/div/div[1]/h1")).getText();
Assert.assertEquals("Furniture & Mattresses", result);
}
}
I used a very obvious element (Furniture & Mattresses) on the page to assert against its text
I want to use JavaScript for my script.
I have created an object of JavaScriptExecutor, but executeScript() method is not present. It shows error when I use executeScript() method.
This is the code I have used:
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.JavascriptExecutor;
public class GetDomain_JS {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
driver.manage().window().maximize();
System.out.println(driver.getCurrentUrl());
JavaScriptExecutor js=(JavaScriptExecutor) driver;
String domain_name=(String) js.executeScript("return document.domain");
System.out.println(doamin_name);
}
}
It works for me; you had a mistake on JavaScriptExecutor with upper case S. Instead, you should have javascriptExecutor with lower case s.
Try this code:
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public class GetDomain_JS {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
driver.manage().window().maximize();
System.out.println(driver.getCurrentUrl());
JavascriptExecutor js=(JavascriptExecutor) driver;
String domain_name=(String) js.executeScript("return document.domain");
System.out.println(domain_name);
}
}
This works for me!! Please thumps up if it does for you!
Please make sure you have imported the correct package.
Expected package for working with Java Script:
import org.openqa.selenium.JavascriptExecutor;
Try this package. This should solve your error.
Explanation:
Add latest jar (I'm using 3.0 beta selenium jar). Import Javascript library package. Take web driver object by casting to JavascriptExecutor and run whatever java script you want to run.
Code:
import com.thoughtworks.selenium.webdriven.JavascriptLibrary;
Object ob = ((JavascriptExecutor) webDriver()).executeScript("return document.domain").toString();
System.out.println(ob);
You can return an Object from executeScript. Later you can get the text out of it.
Object domain_name = js.executeScript("return document.domain");
System.out.println(domain_name.toString());
In this way, you can return values of any type and not just string.
I'm looking to execute a Selenium 2 test against the Nightly browser (FireFox 64bit). It records just fine with the Selenium IDE (v1.8.1). And it also plays back just fine using using the IDE. I then export the code to TestNG format. By the way I've loaded up the Webdriver Backed plugin so it exports WebDriver code for the Selenium 2 version. The problem I'm having is that when I export the code to TestNG format (Java) and execute it, the asserts never find the text on the screen. It executes fine so its not that the code didn't convert. It just seems to be something with the asserts. If I play it from the IDE plugin it finds it the text and asserts just fine, however as soon as it executes in Java it fails all the assertions. Any ideas to what might be going on. My code is below. Thanks much!
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static junit.framework.Assert.*;
import com.thoughtworks.selenium.Selenium;
public class TestWithConfig {
WebDriver driver;
Selenium selenium;
#BeforeMethod
public void startSelenium() {
driver = new FirefoxDriver();
selenium = new WebDriverBackedSelenium(driver,
"http://en.wikipedia.org/wiki/Main_Page");
}
#AfterMethod
public void stopSelenium() {
driver.close();
}
#Test
public void testTest() {
selenium.setSpeed("600");
selenium.open("/wiki/Main_Page");
assertTrue("face not found",selenium.isTextPresent("face"));
selenium.click("link=Contents");
selenium.waitForPageToLoad("30000");
assertTrue("Below not found",selenium.isTextPresent("Below"));
selenium.click("link=Toolbox");
selenium.click("link=What links here");
selenium.waitForPageToLoad("30000");
assertTrue("Pages not found",selenium.isTextPresent("Pages that link to"));
selenium.click("link=exact:Talk:Wine");
selenium.waitForPageToLoad("30000");
assertTrue("Some not found",selenium.isTextPresent("Some"));
}
}
Since your using selenium 2 and webdriver, Assert's work a little different. I can see that you using the WebDriverBackedSelenium. However keep in mind. That's not selenium2. That is just a way to ease into selenium 2. I would use something like this.
WebElement tooltip = driver.findElement(By.xpath("the xpath of the element"));
assertNotNull("Name:","IP Address:",tooltip);
What I'm doing here is. i'm looking for a tooltip.inside that tooltip, there are two main labels that stay the same: Name and IP Address:. So I'm testing to see if those words exists or not in the tool tip. The output should be Name: IP Address:. That tells me the answer is true.