Hi I am trying to test logging into bookmyshow.
Firstly I am reading data from excel sheet to write in username and password but while executing I am getting error that
Error: Main method not found in class Test_Pack1.Bookmyshow, please
define the main method as: public static void main(String[] args)
or a JavaFX application class must extend
javafx.application.Application.
I am providing written program code as well.please look into this and correct me if I am wrong thank you.
package Test_Pack1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
public class Bookmyshow {
//public static void main()
{
System.setProperty("webdriver.chrome.driver",
"G:\\Selenium_Test\\lib\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://in.bookmyshow.com/hyderabad?wzrk_source=Google&wzrk_medium=CPC_Branded&wzrk_campaign=Book_My_Show_Search|RLSA&gclid=CjwKEAiAu6DBBRDDr6-e_6698E0SJACvuxnyBrs85Juwsx_cWiHXrPnS0eKFhZoQmo_nCYgopyDSIxoCGCHw_wcB");
driver.manage().window().maximize();
driver.findElement((By.className("email-input")));
// driver.findElement(By.id("FirstName"));
driver.findElement(By.xpath(".//*[#id='iUserName']")).notifyAll();
}
public static void main(String[] args) throws IOException
{
FileInputStream istream=new FileInputStream("G:\\Selenium_Test\\hari.xlsx");
XSSFWorkbook wrkbook=new XSSFWorkbook(istream);
XSSFSheet sheet1=wrkbook.getSheet("Sheet1");
XSSFRow row=null;
XSSFCell cell=null;
HashMap<String, String> dataHmap = new HashMap<String, String>();
String PropertyName,value;
int lastRowNumber=sheet1.getLastRowNum();
}
}
The way you have commented earlier section is not correct.
First of all, your code which needs to be invoked as a starting point should be included in main method.You can invoke multiple methods in turn from inside main method. I have tried to change your code bit and it should work. Let me know if you face any issue further
package com.automation.servicenow.config;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
public class Bookmyshow {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver",
"G:\\Selenium_Test\\lib\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://in.bookmyshow.com/hyderabad?wzrk_source=Google&wzrk_medium=CPC_Branded&wzrk_campaign=Book_My_Show_Search|RLSA&gclid=CjwKEAiAu6DBBRDDr6-e_6698E0SJACvuxnyBrs85Juwsx_cWiHXrPnS0eKFhZoQmo_nCYgopyDSIxoCGCHw_wcB");
driver.manage().window().maximize();
driver.findElement((By.className("email-input")));
// driver.findElement(By.id("FirstName"));
driver.findElement(By.xpath(".//*[#id='iUserName']")).notifyAll();
FileInputStream istream=new FileInputStream("G:\\Selenium_Test\\hari.xlsx");
XSSFWorkbook wrkbook=new XSSFWorkbook(istream);
XSSFSheet sheet1=wrkbook.getSheet("Sheet1");
XSSFRow row=null;
XSSFCell cell=null;
HashMap<String, String> dataHmap = new HashMap<String, String>();
String PropertyName,value;
int lastRowNumber=sheet1.getLastRowNum();
}
}
Related
But I am getting below error. what needs to be done?
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebElement.getTagName()" because "element" is null
package com.privacyshieldtestcases;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.privacyshieldpageobjects.PrivacyshieldForm;
public class TC_PS_001 extends BaseClass
{
#Test
public void psForm() throws InterruptedException
{
driver.get(url);
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(30));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("pt1:r1:0:it9::content")));
PrivacyshieldForm ps = new PrivacyshieldForm(driver);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
ps.setOrgName(Organizationname);
ps.setein(einnumber);
ps.setunsNum(DbunsNumber);
ps.setadd1(addr1);
ps.setCityName(cityname);
ps.setStateName(statename);
ps.setZipName(zipnum);
ps.setFirstName(fname);
ps.setLastName(lname);
ps.setemailname(emailid);
ps.setemailname(Confirmemailid);
ps.setPhonenum(Pnum);
ps.setSelectpay();
ps.setpaynowbutton();
if(driver.findElement(By.id("pt1:r1:1:s1:it1::content")).isDisplayed())
{
Assert.assertTrue(true);
}
else
{
Assert.assertTrue(false);
}
}}
I can't find a button using id or xpath using pagefactory on expedia.com website. It says no such element: Unable to locate element "tab-flight-tab-flp".
I tried using id and xpath. It gives same error. I wonder this is happening?
It says no such element: Unable to locate element "tab-flight-tab-flp".
I tried using id and xpath. It gives same error. I wonder this is happening?
It says no such element: Unable to locate element "tab-flight-tab-flp".
I tried using id and xpath. It gives same error. I wonder this is happening?
package UsefulPackages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class SearchPageFactory {
WebDriver adriver;
#FindBy(id="tab-flight-tab-flp")
WebElement flightTab;
//constructor
public SearchPageFactory(WebDriver driver) {
this.adriver = driver;
PageFactory.initElements(driver, this);
}
public void clickFlightTab(){
flightTab.click();
}
}
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import UsefulPackages.SearchPageFactory;
public class FrameworkTestCase {
WebDriver achromeDriver;
String abaseUrl;
SearchPageFactory apagefactory;
#Before
public void setUp() throws Exception {
abaseUrl = "http://www.expedia.com";
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\ChromeDirver\\chromedriver.exe");
achromeDriver = new ChromeDriver();
apagefactory = new SearchPageFactory(achromeDriver);
chromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
achromeDriver.manage().window().maximize();
System.out.println("setup completed");
}
#Test
public void test() {
achromeDriver.get(abaseUrl);
WebDriverWait awaittime = new WebDriverWait(achromeDriver, 10);
System.out.println("page factory created");
apagefactory.clickFlightTab();
}
}
Is you want get Flights button ?
It will achieve with :
Find by id :
#FindBy(id="tab-flight-tab-hp")
Find by xpath :
#FindBy(xpath="//button[#id='tab-flight-tab-hp']")
This question already has answers here:
FileUtils not showing suggestion to import predefined class for Screenshot functionality in selenium WebDriver
(5 answers)
Closed 4 years ago.
When I try to get screen shots with following code it is showing error.
FileUtils.copyFile(source, new File("*./Screenshots/facebook.png"));
error message
but when i try the following code it is okay.
FileHandler.copy(source,new File("*./Screenshots/facebook.png"));
Why is that?
full code is below
package sample.code;
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;
import org.testng.annotations.Test;
public class ScreenShot {
#Test
public void facebookScreenShot() throws IOException {
WebDriver driver= new ChromeDriver();
driver.get("http://www.facebook.com");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[#id='u_0_m']")).sendKeys("screenshot");
TakesScreenshot ts=(TakesScreenshot)driver;
File source=ts.getScreenshotAs(OutputType.FILE);
FileHandler.copy(source,new File("*./Screenshots/facebook.png"));
driver.quit();
}
}
By using Robot class ,you can take screenshot.Following is the code to take screenshot.
import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class CaptureScreenshot {
public static void main(String[]args) throws IOException, HeadlessException, AWTException
{
// This code will capture screenshot of current screen
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
// This will store screenshot on Specific location
ImageIO.write(image, "png", new File("C:\\Users\\Screenshot\\CurrentScreenshot.png"));
}
}
I get an error saying 'An invalid or illegal selector was specified'. Note:- in the last findElement statement, I have given the cssSelector as ("//td[id*='verificationMsg']/p[2]) because the id is the closest unique locator I could find to capture the data. And that part of it doesn't change. eg:- My current id value is id="m_7959323670053369637verificationMsg" where the number keeps changing in the recurring emails but "verificationMsg" part of it doesn't.
this is in reference to my previous question and the suggestions that I got
package Amazon;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
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.openqa.selenium.interactions.Actions;
public class VerifyAmazonSignInPage {
public static void main(String[] args) throws AWTException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C://Selenium jars/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.gmail.com");
driver.findElement(By.id("identifierId")).sendKeys("gaurav.thantry#gmail.com");
Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_ENTER);
rob.keyRelease(KeyEvent.VK_ENTER);
driver.findElement(By.name("password")).sendKeys("******");
rob.keyPress(KeyEvent.VK_ENTER);
rob.keyRelease(KeyEvent.VK_ENTER);
driver.findElement(By.xpath("//span[text()='Amazon password assistance']")).click();
String v = driver.findElement(By.cssSelector("td[id*='verificationMsg']/p[2]")).getText();
System.out.println(v);
}
}
Scratch it guys. I got it. //*[contains(#id,'verificationMsg')]/p[2].
This works.
Using Selenium, I have written the below code to read data from an Excel sheet and key in an email and password into Facebook's login page. Error is Unhandled expression to use try/catch. How can I resolve this?
package Excelpack;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ClassTwo {
WebDriver driver;
String username;
String password;
#Test(dataProvider="testdata")
public void testFireFox(String uname, String password1){
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys(uname);
driver.findElement(By.id("pass")).clear();
driver.findElement(By.id("pass")).sendKeys(password1);
}
#DataProvider(name="testdata")
public Object[][] TestDataFeed1()
{
FileInputStream fis=new FileInputStream("D:\\book3.xlsx");
XSSFWorkbook wb=new XSSFWorkbook(fis);
XSSFSheet sh1= wb.getSheetAt(0);
int numrow = sh1.getLastRowNum()-sh1.getFirstRowNum();
int colnum =sh1.getRow(1).getLastCellNum()+1;
Object [][] facebookdata=new Object[numrow][colnum];
for(int i=0;i<numrow;i++)
{
facebookdata[i][0]=sh1.getRow(0).getCell(i).getStringCellValue();
facebookdata[i][1]=sh1.getRow(1).getCell(i).getStringCellValue();
}
return facebookdata;
}
#BeforeTest
public void Setup(){
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
}
}
If you are using any IDE like eclipse or intellijI then IDE would prompt you with the solution as to add "throws necessary exception" or write the block of code in try/catch. from the looks of the code it seems you would need to declare throws exception on your dataprovider TestDataFeed1.