Can seem to make this code execute. Compiler isn't instantiating the driver. what can I do to correct this.
package mypackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
org.openqa.selenium.support.ui.ExpectedConditions;
import
org.openqa.selenium.support.ui.WebDriverWait;
public class selenium {
public static void main(String[] args) {
WebDriver driver = (WebDriver) new FirefoxDriver();
WebDriverWait MyWaitlVar= new
WebDriverWait(driver, 10);
String baseUrl =
"http://newtours.demoaut.com";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = "";
// launch Firefox and direct it to the Base URL
driver.get(baseUrl);
// get the actual value of the title
actualTitle = driver.getTitle();
/*
* compare the actual title of the page
witht the expected one and print
* the result as "Passed" or "Failed"
*/
if (actualTitle.contentEquals
(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
//close Firefox
driver.close();
// exit the program explicitly
System.exit(0);
}
}
Related
The setValue method putting value in the field too fast and losing some chars during a process.
sendKeys method doesn't work correctly too.
[https://pp.userapi.com/c849532/v849532534/1d4fe6/BEVb5_C3O8E.jpg][]
Appium Server 1.13.0
package appiumtests2;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
import static java.lang.Thread.sleep;
public class Stoloto {
/*
* static WebDriver driver;
* AndroidDriver driver;
*/
static AppiumDriver<MobileElement> driver;
public static void main(String[] args) {
try {
Stoloto stoloto = new Stoloto();
stoloto.openStoloto();
} catch (Exception exp) {
System.out.println(exp.getCause());
System.out.println(exp.getMessage());
exp.printStackTrace();
}
}
#Test
public void openStoloto() throws Exception {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "Pixel XL");
cap.setCapability("udid", "HT6B70200690");
cap.setCapability("platformName", "Android");
cap.setCapability("platformVersion", "9");
cap.setCapability("appPackage", "ru.stoloto.mobile");
cap.setCapability("appActivity", "ru.stoloto.mobile.ca.presentation.ui.activity.MainActivity");
URL url = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AppiumDriver<MobileElement>(url, cap);
System.out.println("Application Started...");
MobileElement onBoardContinue = driver.findElement(By.id("ru.stoloto.mobile:id/confirm"));
onBoardContinue.click();
onBoardContinue.click();
MobileElement onBoardLogin = driver.findElement(By.id("ru.stoloto.mobile:id/login"));
onBoardLogin.click();
MobileElement loginField = driver.findElement(By.id("ru.stoloto.mobile:id/user"));
String login = "testtesttest#gmail.com";
sleep(1000);
loginField.setValue(login);
MobileElement passwordField = driver.findElement(By.id("ru.stoloto.mobile:id/passwordInputEdit"));
String password = "qwertyqwerty";
sleep(1000);
passwordField.setValue(password);
driver.hideKeyboard();
MobileElement log_in = driver.findElement(By.id("ru.stoloto.mobile:id/buttonSubmit"));
log_in.click();
System.out.println("Test Completed");
}
}
I need to find a way to set value with a little delay or the other way, which help me to solve this problem.
You can use mobile:shell command for sending text to the device via adb shell
Map<String, Object> argv = new HashMap<>();
argv.put("command", "input");
argv.put("args", Lists.newArrayList("text", "your_text_here"));
driver.executeScript("mobile: shell", argv);
Alternatively you can consider using elementSendText function avaiable via Appium SeeTest Extension
driver.executeScript("seetest:client.elementSendText(\"NATIVE\", \"id=your_element_id\", \"0\", \"your_text_here\")");
try to use WebDriverWait in order to set the value
Naukari error img: I am getting This error on naukari page but on doing manually it does not appear
I am trying to automate Naukari.com so that it gets updated daily on its own instead of me visiting the website daily to do it. My script is as follows:
package naukariUpdate;
import java.util.Set;
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import naukariLoginPOM.LoginPOM;
public class NaukariUpdater {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","./driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.naukri.com/");
driver.getTitle();
String mainWindowTitle = driver.getTitle();
String mainWindowID = driver.getWindowHandle();
Set<String> s = driver.getWindowHandles();
for (String handleID : s)
{
driver.switchTo().window(handleID);
System.out.println(driver.getTitle());
String windowID = driver.getTitle();
if (!windowID.equals(mainWindowTitle))
{
driver.close();
}
}
driver.switchTo().window(mainWindowID);
driver.findElement(By.xpath("(//div[text()='Login'])[1]")).click();
driver.findElement(By.xpath("//a[.='Google']")).click();
Set<String> window = driver.getWindowHandles();
System.out.println();
for (String handleID : window)
{
driver.switchTo().window(handleID);
String windowTitle = driver.getTitle();
System.out.println(windowTitle+"\t"+mainWindowTitle);
if (!windowTitle.equals(mainWindowTitle))
{
System.out.println("in IF");
driver.switchTo().window(handleID);
break;
}
}
System.out.println("out of switching "+driver.getTitle());
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement webElement = driver.findElement(By.xpath("//input[#id='identifierId']"));
wait.until(ExpectedConditions.visibilityOf(webElement));
LoginPOM POM = new LoginPOM(driver);
WebElement logIN = POM.getLogIn();
logIN.sendKeys("Sorry type in your own email ID bro");
WebElement nextBtn = POM.NextButton();
nextBtn.click();
wait.until(ExpectedConditions.visibilityOf(POM.getPassword()));
WebElement pswd = POM.getPassword();
pswd.sendKeys("Sorry type in your own password bro");
nextBtn.click();
}
}
This is the POM:
package naukariLoginPOM;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class LoginPOM {
public LoginPOM(WebDriver driver)
{
PageFactory.initElements(driver , this);
}
#FindBy(xpath = "//input[#id='identifierId']")
private WebElement LogIn;
public WebElement getLogIn()
{
return LogIn;
}
#FindBy(xpath = "//input[#type='password']")
private WebElement Pswd;
public WebElement getPassword()
{
return Pswd;
}
#FindBy(xpath = "//content[.='Next']")
private WebElement NextBtn;
public WebElement NextButton()
{
return NextBtn;
}
}
I don't get where I made the mistake. Everything seems to run fine, yet I get error 1001 in the end. This does not happen when I carry out the process manually by clicking and typing. How can I resolve it?
Error #1001 is an issue with adobe flash player. You will need to redeploy (reinstall) your adobe flash player as it either missing critical libraries or those libraries are corrupted. This is a documented error with Adobe. You can read more about the issue from this link here:
https://forums.adobe.com/thread/258374?start=0&tstart=0
Unfortunately, this is not an issue that is related to Selenium.
The solution is to reinstall your adobe flash player. If your permissions on your workstation are not high enough to do this, ask your IT team.
I have 2 packages, 1st the Base package package ceccms.automation.framework and another package package ceccms.testcases as follows:
package ceccms.automation.framework;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class UniversalMethods {
public static WebDriver driver = null;
public static String chromepath = "D:\\Automation\\Web Drivers\\ChromeDriver\\chromedriver.exe";
public static String edgepath = "D:\\Automation\\Web Drivers\\EdgeDriver\\MicrosoftWebDriver.exe";
public WebDriver openBrowser (String browser, String url) {
if (browser != null) {
switch (browser) {
case "Mozilla":
driver = new FirefoxDriver();
driver.get(url);
break;
case "Chrome":
System.setProperty("webdriver.chrome.driver", chromepath);
driver = new ChromeDriver();
driver.get(url);
break;
case "Edge":
System.setProperty("webdriver.edge.driver", edgepath);
driver = new EdgeDriver();
driver.get(url);
break;
default:
System.out.println("Wrong Browser Name");
driver.quit();
}
}
driver.manage().window().maximize();
return driver;
}
}
and
package ceccms.testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import ceccms.automation.framework.UniversalMethods;
public class LoginTest {
public static String url = "https://test.ceccms.com/Login.aspx?";
static UniversalMethods U = new UniversalMethods();
public static void main(String[] args) throws InterruptedException {
String browserName = "Chrome";
U.openBrowser(browserName, url);
WebElement userName = driver.findElement(By.id("txtUserName"));
userName.sendKeys("pkumar");
WebElement password = driver.findElement(By.id("txtUserPass"));
password.sendKeys("PassMe33");
Thread.sleep(3000);
driver.quit();
}
}
I am running the code through the second package. I want to use one object driver across the package without using the notation U.driver.findElement() . How can I achieve that ?
One of the solutions, you can add Page Objects, where define Web elements and give your Driver as parameter to initiate Page object class with all web elements. This will allow you to work directly with elements.
You can use it, with this structure(By extending universal method):
package ceccms.testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import ceccms.automation.framework.UniversalMethods;
public class LoginTest extends UniversalMethods {
//You can access driver and method without using object reference
public static String url = "https://test.ceccms.com/Login.aspx?";
public static void main(String[] args) throws InterruptedException {
String browserName = "Chrome";
openBrowser(browserName, url);
WebElement userName = driver.findElement(By.id("txtUserName"));
userName.sendKeys("pkumar");
WebElement password = driver.findElement(By.id("txtUserPass"));
password.sendKeys("PassMe33");
Thread.sleep(3000);
driver.quit();
}
}
I am starting to learn how to handle multiple tabs in a browser using Selenium with Java. looks like my code below is not working.
import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class HandlingWindows {
public static void main(String[] args) throws InterruptedException
{
WebDriver driver= new FirefoxDriver();
driver.get("https://www.facebook.com/");
String parent= driver.getWindowHandle();
System.out.println("Parent Window is"+parent);
//Get Data Policy
WebElement we= driver.findElement(By.linkText("Data Policy"));
//Click Data Policy link
we.click();
//Create an arrayList
ArrayList<String> s1= new ArrayList<String>(driver.getWindowHandles());
for(String s2:s1)
{
if(!(s2.equalsIgnoreCase(parent)))
{
driver.switchTo().window(s2);
Thread.sleep(5000);
System.out.println(driver.getWindowHandle());
System.out.println("get title of window"+driver.getTitle());
}
}
}
}
Please let me know how can I display the title 'Data Policy' without using
getWindowHandles().
getWindowHandles() works pretty fine but before invoking getWindowHandles() you have to induce WebDriverwait as follows :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver= new FirefoxDriver();
driver.get("https://www.facebook.com/");
String parent= driver.getWindowHandle();
System.out.println("Parent Window is"+parent);
driver.findElement(By.linkText("Data Policy")).click();
WebDriverWait wait = new WebDriverWait(driver,5);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> s1= driver.getWindowHandles();
for(String s2:s1)
{
if(!parent.equalsIgnoreCase(s2))
{
driver.switchTo().window(s2);
Thread.sleep(5000);
System.out.println(driver.getWindowHandle());
System.out.println("get title of window"+driver.getTitle());
}
}
Console Output :
Parent Window is4294967297
4294967303
get title of windowData Policy
Just put Thread.sleep(3000); after clicking on Data Policy link.
import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class HandlingWindows {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
//D:\new folde backup\Selenium_project\seleniumproject\testNG_practice\driver
System.setProperty("webdriver.gecko.driver", "./driver/geckodriver.exe");
driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
String parent = driver.getWindowHandle();
System.out.println("Parent Window is" + parent);
// Get Data Policy
WebElement we = driver.findElement(By.linkText("Data Policy"));
// Click Data Policy link
we.click();
// Create an arrayList
Thread.sleep(3000);
ArrayList<String> s1 = new ArrayList<String>(driver.getWindowHandles());
System.out.println(s1);
for (String s2 : s1) {
if (!(s2.equalsIgnoreCase(parent))) {
driver.switchTo().window(s2);
Thread.sleep(5000);
System.out.println(driver.getWindowHandle());
System.out.println("get title of window" + driver.getTitle());
}
}
}
}
Output:-
Parent Window is4294967297
[4294967297, 4294967301]
4294967301
get title of windowData Policy
I'm testing this on my local system and trying to verify user is successfully logged in and landed on correct page.
Getting error when trying to compare String data with webelement data.
`package wnsautomation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class login {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\Users\\orange\\Downloads\\geckodriver.exe");
driver= new FirefoxDriver();
WebDriverWait myWait = new WebDriverWait(driver, 10);
String baseUrl = "http://192.168.1.52:9000";
driver.get(baseUrl);
myWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input")));
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input")).sendKeys("admin#gmail.com");
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[3]/div/div/input")).sendKeys("8JXzwRs4VWeGP0Sy");
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[5]/button")).click();
String expectedtext="Summary";
WebElement actualtext;
actualtext = driver.findElement(By.linkText("/html/body/div[3]/div/ng-include/div/div/div[1]/div/h3"));
if (actualtext.contentEquals(expectedtext)){
System.out.println("User succesfully loggedIN");
} else {
System.out.println("Invalid credtendials!!");
}
}
}`
Instead of using Webelement, use String with getText()method so, it will give you the actual String. Not the Webelement.
Note: Instead of using absolute xpath, use relative xpath. For more details on xpath tutorial, refer this link.
String expectedtext="Summary";
String actualtext = driver.findElement(By.xpath("//div/h3[text()='Summary']")).getText();
System.out.println(actualtext);
if (expectedtext.equalsIgnoreCase(actualtext))
{
System.out.println("User succesfully loggedIN");
}
else
{
System.out.println("Invalid credtendials!!");
}
Try this-
String expectedtext="Summary";
String actualtext= null;
actualtext = driver.findElement(By.xpath("/html/body/div[3]/div/ng-include/div/div/div[1]/div/h3")).gettext();