verify search working proprly using Selenium - selenium-webdriver

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

Related

how to select the one suggestion randomly among the five suggestions from google search box

By using this code i can click one suggestion from all suggestions. I cannot click the suggestions randomly. I need to click one suggestion randomly and verify only the clicked the suggestion is displayed or not.
package google;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Google {
public WebDriver driver;
#Test(priority=1)
public void Firefoxaccess() throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:/Users/naveenkumar.d/Downloads/geckodriver-v0.17.0-win64/geckodriver.exe");
driver=new FirefoxDriver();
driver.get("https://www.google.co.in/");
driver.findElement(By.id("lst-ib")).sendKeys("n");
Thread.sleep(3000);
driver.findElement(By.xpath("/html/body/div/div[3]/form/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/li[1]/div/div[2]")).click();
}
}
You could compose a selector based on a rand method.
For example:
r = generate random number here
and the selector should be something like:
//ul[#role='listbox']/li[r]
As for how to check that the option was selected you need to check that the search input has your text.
Text should be what you typed concatenated with the text from your random option, meaning the text from //ul[#role='listbox']/li[r]//b
And the selector for the check: //input[#name='q'][contains(#value, 'your_text')]

Compilation error in Selenium Webdriver : field package.Classfile.driver is not visible

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.

unable to locate a element thru Selenium webdriver

I am trying to write a code to find cheapest tickets between two locations .I am stuck in the beginning of the code because selenium does not recognise simple input text box . here is the code .
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class orbitzsimpleTest {
public static void main(String args[]) throws InterruptedException {
WebDriver dr = new FirefoxDriver();
dr.navigate().to("https://www.orbitz.com/");
Thread.sleep(5000);
System.out.println("after threat sleep");
dr.findElement(By.xpath(".//*[#id='flight-origin']")).sendKeys("SFO");
}
}
I also tried to write the XPath using contains like
List<WebElement> s = dr.findElements(By.xpath(".//*[contains(#id,'flight_*')]"));
No luck. It looks so simple but i'm struggling to go past this.
You can just get this element with dr.findElement(By.id('flight-origin')) or (By.cssSelector('#flight-origin')).
And it's better to use 'smart' waits (wait for element to be displayed/clickable/visible) instead of sleep. Because it can be that 5 secs is not enough and you try to send key to the input which isn't loaded yet.
I think i found the reason why it was not recognising that element. It's because the flight tab had to be selected before it finds this text box .I was able to fix it .
Thank you so much for your input .

How do I get Selenium 2 webdriver to work with Nightly (Firefox 64 bit)

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.

NullPointerException in WebDriverBackedSelenium

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

Resources