I tried Winium but xpath is not working. Is there anything besides Winium?
public class Desktop_Steps {
public static void main (String[] args) throws MalformedURLException, InterruptedException {
DesktopOptions option = new DesktopOptions();
option.setApplicationPath("location of app");
URL url = new URL(" http://127.0.0.1:4723/");
WiniumDriver driver = new WiniumDriver(url,option);
// driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
Thread.sleep(7000);
driver.findElement(By.id("15894400")).sendKeys("hello");
}
}
Related
public class Test{
static WebDriver driver;
public static void main(String[] arg) throws IOException, InterruptedException {
Test2();
}
public static void CallBrowserChrome() {
try {
System.setProperty("webdriver.chrome.driver", "G:\\Chrome\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://google.com");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void Test2() throws IOException, InterruptedException {
CallBrowserChrome();
FileInputStream fis = new FileInputStream("G:\\Book1.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
int rows = sheet.getLastRowNum();
System.out.println(rows);
for (int i = 1; i <= rows; i++) {
String value = sheet.getRow(i).getCell(0).getStringCellValue();
System.out.println(value);
**driver.findElement(By.xpath("//*[#id='js-main-container']"))
.sendKeys(value);**
}
}
}
//The Error is at this line Highlighted(driver in 2nd Class); I have tried giving the WebDriver inside the body of the method but that also does not work
[1]: https://i.stack.imgur.com/0GzeA.png
This line: WebDriver driver = new ChromeDriver(); declares the driver local to CallBrowserChrome and ignores your other declaration.
Change your first method to this:
public static void CallBrowserChrome() {
try {
System.setProperty("webdriver.chrome.driver", "G:\\Chrome\\chromedriver.exe");
driver = new ChromeDriver();// update this line
driver.get("https://google.com");
//...etc
This is My setup class:
public class SetupClass {
//AppiumDriver<MobileElement> driver;
AndroidDriver<MobileElement> d;
public Properties prop =null;
public File file;
public FileInputStream fis;
#Parameters(value ="Android")
#BeforeSuite
public void setup(String Android) throws IOException, Exception{
LoadPropertiesFile();
DesiredCapabilities caps = new DesiredCapabilities();
/*caps.setCapability("plateformName", "Android");
caps.setCapability(CapabilityType.PLATFORM_NAME, "Android");*/
if (Android.equalsIgnoreCase("Moto_g4_Plus")) {
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.0");
caps.setCapability(MobileCapabilityType.UDID, "ZY223WZSQ9");
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Moto G4 Plus");
}
caps.setCapability("app", "C:\\Users\\amarjeet.sharma\\eclipse-workspace\\com.asm.app\\src\\test\\resources\\app\\asmVisit.apk");
caps.setCapability("appPackage", "com.sumasoft.net.asmscheduler");
caps.setCapability("appActivity", "md5f32326382a711c73d2de951d70f3bd5e.MainActivity");
caps.setCapability("autoGrantPermissions", true);
URL url;
try {
url = new URL("http://127.0.0.1:4723/wd/hub");
//driver = new AppiumDriver<MobileElement>(url, caps);
d = new AndroidDriver<MobileElement>(url, caps);
Thread.sleep(5000);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
System.out.println("Cause is:" +e.getCause());
System.out.println("Message is:" +e.getMessage());
e.printStackTrace();
}
}
public void LoadPropertiesFile() throws IOException {
prop=new Properties();
file =new File(System.getProperty("user.dir")+"/src/main/resources/config.properties");
fis=new FileInputStream(file);
prop.load(fis);
//System.out.println(prop.getProperty("userId"));
}
//#AfterTest
public void TearDown() throws IOException{
System.out.println("Quit");
d.quit();
}
}
This is My LoginTest
public class LoginTest extends SetupClass{
#Test(priority = 1)
public void login() throws Exception {
LoginPage lp = new LoginPage(d);
//System.out.println(prop.getProperty("userId"));
lp.userName(prop.getProperty("userId"));
Thread.sleep(500);
lp.password(prop.getProperty("Passwd"));
Thread.sleep(500);
lp.loginButton();
}
}
And this is my create new journey class
public class AddJourneyTest extends SetupClass{
#Test(priority=2)
public void AddNewJourney() throws Exception {
AddjourneyPage addobj = new AddjourneyPage(d);
addobj.ClicktoAdd();
addobj.SelectFromToDate();
addobj.SelectState();
addobj.SelectCity();
addobj.ClickonSubmit();
}
}
I am getting NullPointerException for AddJourney class
I think when I run it as TestNG then the driver gets initiated for the LoginTest and holds the control. So that AddJourney class does not get driver initiated and hence getting NullPointerException.
I am using Appium Server, Java + Selenium + TestNG for the Android Application automation on Windows system with physical android device.
Maybe I am wrong. But I am not getting solution over it. Please help.
This is my first question ever asked here, so I hope I ask it the right way:
I'm working on a multimodule maven project to test a software that is still in a development. I've been using Cucumber, Selenium, JUnit for some time and doing online courses to improve my skills. In one of the tutorials was explained how to use Driver Factory and configuration files so I applied it to my project, but after that was unable to run any of the drivers (gecko/chrome/ie). Here is a big part of the code, so I hope someone can help :)
I catch the exception from the DriverFactory.class:
"Unable to load browser: null"
config.properties file has only one line:
browser=chrome
public class DriverFactory {
public static WebDriver driver;
public WebDriver getDriver() {
try {
//Read Config
ReadConfigFile file = new ReadConfigFile();
String browserName = file.getBrowser();
switch (browserName) {
case "firefox":
if (driver == null) {
System.setProperty("webdriver.gecko.driver", Constant.GECKO_DRIVER_DIRECTORY);
FirefoxOptions options = new FirefoxOptions();
options.setCapability("marionette", false);
driver = new FirefoxDriver(options);
driver.manage().window().maximize();
}
break;
case "chrome":
if (driver == null) {
System.setProperty("webdriver.chrome.driver", Constant.CHROME_DRIVER_DIRECTORY);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
break;
/*case "ie":
if (driver == null) {
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
System.setProperty("webdriver.ie.driver", Constant.IE_DRIVER_DIRECTORY);
capabilities.setCapability("ignoreZoomSetting", true);
driver = new InternetExplorerDriver(capabilities);
driver.manage().window().maximize();
}
*/
}
} catch (Exception e) {
System.out.println("Unable to load browser: " + e.getMessage());
} finally {
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
}
return driver;
}
public class ReadConfigFile {
protected InputStream input = null;
protected Properties prop = null;
public ReadConfigFile() {
try {
input = ReadConfigFile.class.getClassLoader().getResourceAsStream(Constant.CONFIG_PROPERTIES_DIRECTORY);
prop = new Properties();
prop.load(input);
} catch(IOException e) {
e.printStackTrace();
}
}
public String getBrowser() {
if(prop.getProperty("browser") == null) {
return "";
}
return prop.getProperty("browser");
}
public class Constant {
public final static String CONFIG_PROPERTIES_DIRECTORY = "properties\\config.properties";
public final static String GECKO_DRIVER_DIRECTORY = System.getProperty("user.dir") + "\\src\\test\\resources\\geckodriver.exe";
public final static String CHROME_DRIVER_DIRECTORY = System.getProperty("user.dir") + "\\src\\test\\resources\\chromedriver.exe";
public class CreateCaseSteps extends DriverFactory {
//Background Steps
#Given("^user accesses the login page$")
public void userAccessesTheLoginPage() throws Throwable {
getDriver().get("http://localhost:8080/");
}
#When("^user enters a valid username")
public void userEntersAValidUsername(DataTable table) throws Throwable {
Thread.sleep(3000);
List<List<String>> data = table.raw();
getDriver().findElement(By.xpath("//input[#placeholder='username']")).sendKeys(data.get(0).get(0));
}
#When("^user enters a valid password")
public void userEntersAValidPassword(DataTable table) throws Throwable {
Thread.sleep(3000);
List<List<String>> data = table.raw();
getDriver().findElement(By.xpath("//input[#placeholder='password']")).sendKeys(data.get(0).get(0));
}
#And("^user clicks on the login button$")
public void userClicksOnTheLoginButton() throws Throwable {
getDriver().findElement(By.xpath("//input[#value='Login']")).click();
}
#Then("^user should be taken successfully to the login page$")
public void userShouldBeTakenSuccessfullyToTheLoginPage() throws Throwable {
Thread.sleep(3000);
WebElement logoutMenu = getDriver().findElement(By.xpath("//i[#class='glyphicon glyphicon-user']"));
Assert.assertEquals(true, logoutMenu.isDisplayed());
}
I am trying to use PageFactory Design Pattern but getting Null Pointer Exception.Please suggest any improvement required.
My Sample code looks like:
public DriverCapabilities() throws IOException{
URL url;
try {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("newCommandTimeout", "90");
caps.setCapability("browserName", "");
caps.setCapability("appActivity",Config.appActivity );
caps.setCapability("appPackage", Config.appPackage);
caps.setCapability("appWaitActivity",Config.appWaitActivity);
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "5.0");
caps.setCapability("deviceName", "Android Emulator");
}
caps.setCapability("app", Config.path);
caps.setCapability("appium-version", "1.3.7.2");
url = new URL("http://127.0.0.1:4723/wd/hub");
innerDriver = Config.os.equalsIgnoreCase("ANDROID")? new AndroidDriver(url, caps): new IOSDriver(url, caps);
DriverWait.implicitWait();
PageFactory.initElements(new AppiumFieldDecorator(innerDriver, 5, TimeUnit.SECONDS), this);
}
Sample Screen Class:
public class DemoScreen{
#AndroidFindBy(id = "eula_accept")
private static RemoteWebElement eula;
public static void submit() throws IOException{
init.getInstance(); //To initialize the driver
getEula().click();
}
public static WebElement getEula() {
return eula;
}
}
Sample Test code:
public class Demo1{
#Test(groups={"smokeTest"})
public void test1() throws IOException {
DemoScreen.submit();
}*
Is it because somewhere I'm not initializing the object or has it to do with the design pattern I'm following?
class Login as the following method Kreato_Login():-
public void Kreato_Login(){
driver = new FirefoxDriver();
baseUrl = "https://testrugtn.kreatocrm.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit. SECONDS);
driver.manage().window().maximize();
driver.get(baseUrl + "/");
driver.findElement(By. id("Login_txtUserName")).clear();
driver.findElement(By. id("Login_txtUserName")).sendKeys( "saravana");
driver.findElement(By. id("Login_txtPassword")).clear();
driver.findElement(By. id("Login_txtPassword")).sendKeys( "5678");
driver.findElement(By. id("Login_btnLogin")).click();
}
Class Lead as the following method "Lead_MandatoryCheck()":-
LoginLogout leadInstance=new LoginLogout();
public void Lead_MandatoryCheck() throws InterruptedException{
leadInstance.Kreato_Login();
driver1.findElement(By. xpath("//a[contains(text(),'Customers')]")).click();
driver1.findElement(By. linkText("Leads")).click();
//Add New
driver1.findElement(By. cssSelector( "#ctl00_ContentPlaceHolder1_cbpSubContent_imgAddNew_CD > span.dx-vam" )).click();
Thread.sleep(3000);
//Save
driver1.findElement(By. cssSelector("#ctl00_ContentPlaceHolder1_cbpAssociationNew_btnNewItemTopCreationSave_CD > span.dx-vam" )).click();
String mandatoryPopup= driver1.switchTo().alert().getText();
driver1.switchTo().alert().accept();
System.out.println(mandatoryPopup);
}
I try to call the above methods from the class Trigger as follows:-
public class TriggerClass {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
/*LeadCreation lc= new LeadCreation();
lc.setUp();
lc.testLeadCreation();
lc.tearDown();*/
LeadModule Lm=new LeadModule();
Lm.Lead_MandatoryCheck();
}
when i run the "Trigger.class"
Exception in thread "main" java.lang.NullPointerException
at workflow.LeadModule.Lead_MandatoryCheck(LeadModule.java:132)
at workflow.TriggerClass.main(TriggerClass.java:13)
This is how you should structure your class, members and functions:
TriggerClass.java:
public class TriggerClass {
public static WebDriver driver;
public static void main(String[] args) throws Exception {
driver = new FirefoxDriver();
LeadModule Lm=new LeadModule();
Lm.Lead_MandatoryCheck();
}
LeadModule.java:
public class LeadModule {
public WebDriver driver;
public LeadModule() {
this.driver = TriggerClass.driver;
}
public void Lead_MandatoryCheck() throws InterruptedException{
LoginLogout leadInstance = new LoginLogout();
leadInstance.Kreato_Login();
driver.findElement(By.xpath("//a[contains(text(),'Customers')]")).click();
driver.findElement(By.linkText("Leads")).click();
//Add New
driver.findElement(By.cssSelector( "#ctl00_ContentPlaceHolder1_cbpSubContent_imgAddNew_CD > span.dx-vam" )).click();
Thread.sleep(3000);
//Save
driver.findElement(By. cssSelector("#ctl00_ContentPlaceHolder1_cbpAssociationNew_btnNewItemTopCreationSave_CD > span.dx-vam" )).click();
String mandatoryPopup= driver.switchTo().alert().getText();
driver.switchTo().alert().accept();
System.out.println(mandatoryPopup);
}
LoginLogout.java:
public class LoginLogout extends LeadModule {
public void Kreato_Login(){
baseUrl = "https://testrugtn.kreatocrm.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit. SECONDS);
driver.manage().window().maximize();
driver.get(baseUrl + "/");
driver.findElement(By.id("Login_txtUserName")).clear();
driver.findElement(By.id("Login_txtUserName")).sendKeys("saravana");
driver.findElement(By.id("Login_txtPassword")).clear();
driver.findElement(By.id("Login_txtPassword")).sendKeys("5678");
driver.findElement(By.id("Login_btnLogin")).click();
}
}
Your Java/Selenium basics are not clear. Please read through the documentation or view related tutorials on YouTube.