Team,
I am stuck in one issue.
I have two classes One where i have created a plain login functionality using constructor and in second one i am instantiating the login.
The issue is the test case is terminating immediately.
Please help me out
I am giving the below code.
Is there any problem with initialisation.
public Userlogin(String username, String brand, WebDriver driver) {
WebElement user=driver.findElement(By.id("UserNameInputText"));
user.sendKeys(username);
Select select=new Select(driver.findElement(By.id("Brand")));
select.selectByValue(brand);
WebElement login_button=driver.findElement(By.id("CmdLogin"));
login_button.submit();
String actual_title=driver.getTitle();
String expected_title="VSS 4";
if(!(actual_title.matches(expected_title)))
{
Assert.assertFalse(false);
driver.quit();
}
WebElement cancel=driver.findElement(By.id("Cancel"));
if(!driver.findElements(By.id("Cancel")).isEmpty())
{
cancel.click();
}
}
}
I am calling login in below code
package testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import vsslogin.Userlogin;
public class messageboard extends Userlogin {
public messageboard(String username, String brand, WebDriver driver) {
super(username, brand, driver);
// TODO Auto-generated constructor stub
}
WebDriver driver;
#Test
void messageboard()
{
System.setProperty("webdriver.chrome.driver", "C:/Eclipse/chromedriver.exe");
driver=new ChromeDriver();
driver.get("http://153.112.61.197/vss_connect_testr1/Login/Login.aspx?nextview=Welcome");
driver.manage().window().maximize();
Userlogin login=new Userlogin("TYP40US","Mack",driver);
}
}
Try to add System.out.println() statement in your code mentioned below and RUN.
If you have seen the "Close Browser" Message in Console. Tile of the Page is Not Matched with your expected_title.
Line : driver.quit(); is terminates your browser.
if(!(actual_title.matches(expected_title)))
{
System.out.println("Close Browser");
Assert.assertFalse(false);
driver.quit();
}
Related
My code is working until #BeforeMethod and it is opening the URL, but what I'm typing after #Test is not working. It is not typing credentials on the login page, hence Test failed.
package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
// import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Great {
WebDriver driver;
#BeforeMethod
public void setup()
{
System.setProperty("webdriver.chrome.driver", "C:\\Program Files
(x86)\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseurl = "https://getfieldforce.com/dishqa";
driver.get(baseurl);
// Actions act = new Actions(driver);
}
#Test (priority=0)
public void login()
{
System.out.println("Login process starts");
driver.findElement(By.id("email")).sendKeys("123#p.com");
driver.findElement(By.id("password")).sendKeys("123456");
driver.findElement(By.id("cta")).click();
System.out.println("Login Sucessfully");
}
Removing webdriver from this line fixed the issue; WebDriver driver = new ChromeDriver();
Thanks for looking into this :-)
I am unable to type text in login page, tried all the solutions provided online but issue still exists. anyone help me to resolve the issue. below is the code:
package com.individual.newlearn;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class secondProgram {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in/webhp?ie=UTF-8&rct=j&gws_rd=cr&dcr=0&ei=9Wq2WozGFIH4vgSJ2LmoAw");
Thread.sleep(2000);
driver.findElement(By.xpath("//a[text()='Gmail']")).click();
//driver.navigate().to("https://accounts.google.com/signin/v2/identifier?hl=EN&flowName=GlifWebSignIn&flowEntry=ServiceLogin");
driver.findElement(By.xpath("//a[text()='Sign In']")).click();
Thread.sleep(2000);
driver.findElement(By.id("identifierId")).sendKeys("mail id");
java script i used for typing mail id:
/*WebElement searchbox = driver.findElement(By.id("identifierId"));
JavascriptExecutor myExecutor = ((JavascriptExecutor)driver);
myExecutor.executeScript("arguments[0].value='mail id';", searchbox);
Thread.sleep(3000);*/
I'm trying to right click a link in a web page and open it in a new window with context click method of Action class. Below is the code which I got from few references. But this doesn't work. Can someone help to find out what is the mistake I'm doing here?
package webDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class RightClick {
public static void main(String args[]) throws Exception
{
String baseUrl = "https://www.google.com";
System.setProperty("WebDriver.gecko.driver","C://geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get(baseUrl);
WebElement select = driver.findElement(By.linkText("About"));
Actions builder = new Actions(driver);
builder.contextClick(select).perform();
builder.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER);
}
}
Is there any other way to do this?
Thanks in Advance
Try the below code your problem will be solved,Its working I have tried it for you,Kindly revert back if you have got it.
public class Demo {
public static void main(String[] args) throws AWTException {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"/Drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in/?gfe_rd=cr&dcr=0&ei=sxWqWoDHL6SwX7PXjaAH");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
WebElement element=driver.findElement(By.xpath(".//*[#id='fsl']/a[3]"));
String linkToOpen= element.getAttribute("href");
System.out.println(linkToOpen);
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_T);
//To switch to the new tab
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
//To navigate to new link/URL in 2nd new tab
driver.get(linkToOpen);
}
}
When we click a link while pressing Shift key, the resulting link generally opens in new window.
See if this helps:
WebElement link = driver.findElement(By.linkText("About"));
Actions builder = new Actions(driver);
builder.keyDown(Keys.SHIFT).click(link).keyUp(Keys.SHIFT).build().perform();
I want to call login and search method from two different class into the main class. I have written the two diff classes for login and search but when I am calling both method in class containg main method .it gives me an error as below>>
Exception in thread "main" java.lang.NullPointerException
at Test_package_Ted_baker.search.search_1(search.java:14)
at Test_package_Ted_baker.SHopping.main(SHopping.java:16)
Can someone please help me to find out solution about how to call methods from one class to another class. My code is as below>>
Main class
package Test_package_Ted_baker;
import org.openqa.selenium.WebDriver;
public class SHopping {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
login_Ted_Baker obj=new login_Ted_Baker();
obj.login_to_Ted_Baker("ss3777010#gmail.com", "google#123");
System.out.println("Login sucessfully");
search obj1=new search();
obj1.search_1();
obj.user_logout();
System.out.println("User log out sucessfully");
}
}
Login class
package Test_package_Ted_baker;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class login_Ted_Baker {
WebDriver driver;
public static void main (String [] args) throws InterruptedException
{
login_Ted_Baker obj=new login_Ted_Baker();
obj.login_to_Ted_Baker("ss3777010#gmail.com", "google#123");
System.out.println("Login Sucessfully");
obj.user_logout();
}
public void usename( String Username)
{
WebElement Username_TB=driver.findElement(By.name("j_username"));
Username_TB.sendKeys(Username);
}
public void password(String Password)
{
WebElement Password_TB=driver.findElement(By.name("j_password"));
Password_TB.sendKeys(Password);
}
public void submit()
{
driver.findElement(By.xpath(".//*[#id='command']/div/input ")).click();
}
public void login_Ted_Baker(WebDriver driver){
this.driver = driver;
}
public void user_logout() throws InterruptedException
{
Thread.sleep(1000);
WebElement Sign_in_link=driver.findElement(By.xpath(".//*[#id='page']/header/div/nav[1]/ul/li[3]/div/a"));
WebElement Sign_out_button=driver.findElement(By.xpath(".//*[#id='account']/ul/li[2]/a"));
Thread.sleep(1000);
Actions Act1=new Actions(driver);
Act1.moveToElement(Sign_in_link).click();
Act1.moveToElement(Sign_out_button).click().build().perform();
System.out.println("Logout sucessfully");
}
public void login_to_Ted_Baker(String Username,String Password) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver_win32\\chromedriver.exe");
//System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
Thread.sleep(2000);
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
try{
driver.get("http://www.tedbaker.com/");
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")));
if(driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")).isDisplayed())
{
driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")).click();
}
driver.findElement(By.xpath(".//*[#id='page']/header/div/nav[1]/ul/li[3]/div/a")).click();
Thread.sleep(2000);
this.usename(Username);
this.password(Password);
this.submit();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
searching class
package Test_package_Ted_baker;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class search {
WebDriver driver;
public void search_1() throws InterruptedException
{
WebElement Search_Item=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[1]/a/span[1]"));
Search_Item.click();
WebElement Seacrh_item_message=driver.findElement(By.id("search"));
WebElement Search_textbox=driver.findElement(By.xpath("html/body/div[2]/header/div/form/div[1]/ol/li[1]/input"));
Search_textbox.sendKeys("Watches");
driver.findElement(By.xpath("html/body/div[2]/header/div/form/div[1]/ol/li[2]/input")).click();
WebElement SearchResult_pagemessage=driver.findElement(By.xpath("html/body/div[2]/div/div/div[1]/div/h1"));
driver.findElement(By.xpath("html/body/div[2]/div/div/div[1]/div/div/div/span[1]")).click();
WebElement Item_Tobuy = driver.findElement(By.xpath("html/body/div[2]/div/div/div[2]/div[2]/div[1]/article/div[2]/header/div/h4/a"));
JavascriptExecutor jse1= (JavascriptExecutor)driver;
jse1.executeScript("window.scrollBy(0,400)", "");
Item_Tobuy.click();
driver.findElement(By.xpath("html/body/div[2]/div/div/div[2]/div[1]/section[2]/form/ol/li[2]/span/span/select")).click();
Select dropdown1=new Select(driver.findElement(By.id("qty")));
dropdown1.selectByVisibleText("1");
driver.findElement(By.id("qty")).click();
// driver.findElement(By.xpath("*[#classname='button add_to_cart major full_mobile colour_dark']/div[2]/div[1]/div/input[1]")).click();
// driver.findElement(By.xpath("//*[#id='add_to_cart_form']/div[2]/div[2]/a/span[1]/span[1]")).click();
driver.findElement(By.xpath("//input[contains(#class,'add_to_cart')]")).click();
Thread.sleep(1000);
Actions act=new Actions(driver);
WebElement My_cart_button=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[3]/a/span[2]"));
//WebElement My_cart_button=driver.findElement(By.xpath("//input[contains(#class,'My bag')]"));
WebElement View_bag_checkout_button=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[3]/div/ul/li/a"));
act.moveToElement(My_cart_button).moveToElement(View_bag_checkout_button).click().build().perform();
driver.findElement(By.xpath("html/body/div[2]/div/div/div/section/table/tbody/tr/td[2]/form/ul/li[2]/a")).click();
String Cart_empty_mesg=driver.findElement(By.xpath("html/body/div[1]/div/div/div/header/h1")).getText();
System.out.println(Cart_empty_mesg);
String Actual_cart_empty_msg="Your shopping bag is empty";
if(Cart_empty_mesg==Actual_cart_empty_msg)
{
System.out.println("Cart is empty, you can add product cart");
}
}
}
This might sound harsh, but first of all you should read about
Null Pointer Exception from this link.
How to debug from a StackTrace from this link.
Why using absolute xpath, or xpaths locators in general is a bad strategy from here.
From your stack trace,
Exception in thread "main" java.lang.NullPointerException at
Test_package_Ted_baker.search.search_1(search.java:14) at
Test_package_Ted_baker.SHopping.main(SHopping.java:16)
I see that your code in Search class
WebElement Search_Item=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[1]/a/span[1]"));
is not correct. I suspect your locator is not correct for this one. The element can easily be identified using an id attribute, as
driver.findElement(By.id("search"));
or , if you're using xpaths, then
driver.findElement(By.xpath("//*[#id="search"]"));
should do away the NPE - but not sure since you've used a lot of absolute xpaths - which is a horrible practise.
My scenario is, when I hit the URL the page should navigate to the authentication window and after enter the valid user ID and password, the main page gets displayed and upon clicking any link in the main page, a new window will open and displays the corresponding page without any authentication.
Issue : when I use the switchTo() in the code, clicking the link from the main page is opening a new window and again prompting me to enter the user ID and pass.
If I remove that switchTo(), upon clicking it is taking me to the expected page wihout authentication.
**Code** import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.junit.*;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class MKTEvent {
public WebDriver driver;
public String baseUrl;
public void testMKTEvent() throws IOException, BiffException, InterruptedException {
driver = new InternetExplorerDriver();
baseUrl = "<<URL>>";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl);
File f=new File("D:\\User ID.xls");
Workbook w=Workbook.getWorkbook(f);
Sheet s=w.getSheet(0);
for(int i=1;i<s.getRows();i++)
{
String uname=s.getCell(0,i).getContents();
String pass=s.getCell(1,i).getContents();
driver.findElement(By.name("j_username")).clear();
driver.findElement(By.name("j_username")).sendKeys(uname);
driver.findElement(By.name("j_password")).clear();
driver.findElement(By.name("j_password")).sendKeys(pass);
driver.findElement(By.id("submit")).click();
Thread.sleep(15000);
driver.findElement(By.id("Dashboard")).click();
String parentWindow = driver.getWindowHandle();
System.out.println(parentWindow);
for(String windowHandle : driver.getWindowHandles()){
if(!windowHandle.equals(parentWindow))
{
driver.switchTo().window(windowHandle);
String nt = driver.findElement(By.id("createRequest")).getText();
System.out.println(nt);
driver.findElement(By.id("createRequest")).click();
}
}
}
}
}
Please help me to fix this issue.
Using DesiredCapabilities will solve your issue.
DesiredCapabilities IECapabilities = DesiredCapabilities.internetExplorer();
IECapabilities.setCapability("enablePersistentHover", false);
IECapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
IECapabilities.setCapability("ignoreProtectedModeSettings", true);
IECapabilities.setCapability("ie.ensureCleanSession", false);