clear method not working with testng but working without it - java

.clear() in my test script is not working with testng version 6.14.2 but when i am running the same code without testng the clear method is working as expected.
i am running the code as mentioned below:
driver.findElement(By.id("email")).clear();
But this loc is not performing any action.
Blockquote

clear() is working for me. refer the following snippet
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Testngexample {
private WebDriver driver;
#BeforeClass
public void setUp() {
System.setProperty("webdriver.chrome.driver","Add chromedriver path chromedriver.exe");
driver = new ChromeDriver();
}
#AfterClass
public void tearDown() {
driver.quit();
}
#Test
public void GoogleEarch() throws InterruptedException {
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.name("q")).click();
driver.findElement(By.name("q")).sendKeys("testing");
Thread.sleep(5000);
driver.findElement(By.name("q")).clear();
Thread.sleep(5000);
driver.close();
}
}

Related

Unable to run testng program

I am unable to run a testng program as it gives classpath error. I have already added testng libraries:
org.testng.TestNGException:
Cannot find class in classpath: testngBasic
package mobileAutomation;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class testngBasic {
WebDriver driver;
#BeforeMethod
public void setUP() {
System.setProperty("webdriver.chrome.driver", "C:\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.google.com");
}
#Test
public void getTitle() {
String googleTitle = driver.getTitle();
System.out.println(googleTitle);
}
#AfterMethod
public void closeBrowser() {
driver.quit();
}
}
Just do Eclipse > Project > Clean and then run the test cases again. It should work.

"Cannot instantiate class" error while executing tests through Selenium Java TestNG

I'm using Selenium and TestNG for the first time and I've been trying to search an element by its ID but I keep getting an "Cannot instantiate class" error. This is my code:
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
public class NewTesting {
WebDriver driver = new FirefoxDriver();
#BeforeTest
public void setUp() {
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
Maybe I missed installing something? I installed the TestNG plug-in for eclipse and added the WebDriver JAR files, do I need to do more?
I tried following multiple tutorials but I keep getting errors, I hope someone can help. Thanks in advance!
EDIT:
I now have this:
public class NewTest {
private WebDriver driver;
#BeforeTest
public void setUp() {
System.setProperty("webdriver.gecko.driver","C:\\Program Files\\Selenium\\FirefoxDriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
It does open the website now but I'm getting a nullpointer exception now:
FAILED CONFIGURATION: #AfterTest tearDown
java.lang.NullPointerException
at NewTest.tearDown(NewTest.java:21)
Replace this set of imports:
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
With:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
Additionally, you have to download the required format of GeckoDriver executable from mozilla/geckodriver, extract the binary and then initialize the FirefoxDriver.
Your effective code block will be:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
public class NewTesting {
WebDriver driver;
#BeforeTest
public void setUp() {
System.setProperty("webdriver.gecko.driver","C:\\path\\to\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://book.theautomatedtester.co.uk/chapter1");
}
#AfterTest
public void tearDown() {
driver.quit();
}
#Test
public void testExample() {
WebElement element = driver.findElement(By.id("verifybutton"));
}
}
If you're on windows, this previous question may be some help to you.
It mentions that you can download geckodriver, and then initialize your FirefoxDriver like this:
System.setProperty("webdriver.gecko.driver","G:\\Selenium\\Firefox driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();

When i want to run this program instead of run as application it will shown like "run as configuration". Why it will ask like this

I am very new to the selenium section please check this code in that when I want to run this application at its not run. Instead of running it will it will ask like run as configuration.
package com.shiftwizard.application;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Login {
public WebDriver driver;
public void positive()
{
System.setProperty("webdriver.chrome.driver", "D:\\prasanth softwares\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://devtest-new.myshiftwizard.com");
driver.findElement(By.name("txtUserName")).sendKeys("cs#shiftwizard.com");
driver.findElement(By.name("txtPassword")).sendKeys("P#ssword!1");
driver.findElement(By.name("btnLogin1")).click();
}
public void negitive()
{
System.setProperty("webdriver.chrome.driver", "D:\\prasanth softwares\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.findElement(By.name("txtUserName")).sendKeys("cs#shiftwizard.com");
driver.findElement(By.name("txtPassword")).sendKeys("P#ssword1");
driver.findElement(By.name("btnLogin1")).click();
System.out.println(driver.findElement(By.id("reqPass")));
}
public void close()
{
driver.close();
}
}
This is my code while i want to run this application i get like run as configuration listed of the run as java application.
Try this:
// set you package name here
package Prabha;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Login {
public static WebDriver driver;
WebDriverWait wait5s = new WebDriverWait(driver,5);
#BeforeClass
public static void setUpClass() {
// set your exe location here
System.setProperty("webdriver.chrome.driver", "C:\\Users\\pburgr\\Desktop\\chromedriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
// set your profile folder here or remove this line
options.addArguments("user-data-dir=C:\\Users\\pburgr\\AppData\\Local\\Google\\Chrome\\User Data");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
}
#Before
public void setUp() {}
#After
public void tearDown() {}
#AfterClass
public static void tearDownClass() {driver.close();driver.quit();}
#Test
public void login() throws InterruptedException {
// calling method "positive", the method "negative" still unused in this class
positive();
}
public void positive() throws InterruptedException {
driver.get("https://devtest-new.myshiftwizard.com");
// wait 5 seconds to load the page
wait5s.until(ExpectedConditions.elementToBeClickable(By.name("txtUserName"))).sendKeys("cs#shiftwizard.com");
driver.findElement(By.name("txtPassword")).sendKeys("P#ssword!1");
driver.findElement(By.name("btnLogin1")).click();
Thread.sleep(5000);
// to be continued...
}
public void negative() throws InterruptedException {
driver.get("https://devtest-new.myshiftwizard.com");
// wait 5 seconds to load the page
wait5s.until(ExpectedConditions.elementToBeClickable(By.name("txtUserName"))).sendKeys("cs#shiftwizard.com");
driver.findElement(By.name("txtPassword")).sendKeys("intentionally_wrong_password");
driver.findElement(By.name("btnLogin1")).click();
Thread.sleep(5000);
// to be continued...
}
}
The username seems to be invalid, but I believe you'll manage.

NoSuchMethodError: org.openqa.selenium.remote.CommandInfo.<init>(Ljava/lang/String;Lorg/openqa/selenium/remote/HttpVerb;)V

i'm trying to run selendroid web for practice but i'm getting errors even i update selenium version (2.48.2) and set all paths correctly.
below is my code and i'm getting this error.
Please help me...
NoSuchMethodError: org.openqa.selenium.remote.CommandInfo.(Ljava/lang/String;Lorg/openqa/selenium/remote/HttpVerb;)V
package com.guru.test;
import io.selendroid.SelendroidCapabilities;
import io.selendroid.SelendroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class testWeb {
public WebDriver driver;
#BeforeSuite
public void setUp() throws Exception
{
DesiredCapabilities caps = SelendroidCapabilities.android();
driver = new SelendroidDriver(caps);
}
#Test
public void WebSiteTest() throws Exception
{
driver.get("http://google.com");
WebElement searchQuery = driver.findElement(By.name("q"));
searchQuery.click();
searchQuery.sendKeys("Test");
WebElement submit = driver.findElement(By.name("btnG"));
submit.click();
}
#AfterSuite
public void tearDown() throws Exception{
driver.quit();
}
}
I think you have mixed up selenium and selendroid jars. selendroid-standalone + selendroid-client should be enough for this.

TestNG with IE webdriver

Hi Im a newbie to selenium, i was trying to use TestNG with IE webdriver, Now i cant instantiate the IE driver directly under the class (Not the main method). When i do that i get the below error:
Multiple markers at this line
- Syntax error on tokens, FormalParameter expected instead
- Syntax error on token(s), misplaced construct(s)
- Syntax error on token ""webdriver.ie.driver"", invalid
If i then put on a method with #BeforeSuite annotation, i need to pass the driver to every other test method in the class. Is there a way where i can by pass this passing the driver object.
Find below the sample code i am using:
package FirstTestNGPackage;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class FirstTestNGclass {
#BeforeSuite
public void SetDriverPaths()
{
File IEDriver = new File("C:\\Users\\REDACTED\\Desktop\\SeleniumJars\\IE Driver\\IEDriverServerX64_2.44.0.exe");
System.setProperty("webdriver.ie.driver", IEDriver.getAbsolutePath());
WebDriver Driver = new InternetExplorerDriver();
}
#Test
public void tester()
{
Driver.findElement(By.id("keywordInput")).sendKeys("REDACTED");
Driver.findElement(By.id("keywordInput")).sendKeys(Keys.ENTER);
Assert.assertEquals(Driver.findElement(By.xpath(".//*[#id='search_results']/h1/span/strong")).getText(), "REDACTED");
}
#BeforeTest
public void RMSLogin()
{
Driver.navigate().to("REDACTED");
}
#AfterTest
public void closeBrowser()
{
Driver.quit();
}
}
init you webdriver like this then you can use it in every method with this.driver.
public class FirstTestNGclass {
public WebDriver driver;
#BeforeSuite
public void SetDriverPaths()
{
// ....
this.driver = new InternetExplorerDriver();
}
// ....
}
One of the simple solution to this problem is to use a common driver class for your Test suite...so that we can use the same driver instance in all classes
Common driver class
public class Driver {
public static WebDriver driver=null;
public static WebDriver startdriver(String browser){
if(browser.equalsIgnoreCase("Chrome")){
System.setProperty("webdriver.chrome.driver", "path");
driver=new ChromeDriver();
}else if(browser.equals("IE")){
System.setProperty("webdriver.ie.driver", IEDriver.getAbsolutePath());
driver=new InternetExplorerDriver();
}
return driver;
}
}
you can create a driver instance like this
Driver.startdriver("IE");
You can use the driver object like classname.instance
Driver.driver.findElement(By.xpath("path"));
Hope this helps you...if you have any queries Kindly get back

Categories

Resources