I am searching for an element in selenium webdriver, via an object file. All other elements are getting returned, apart from one. I have tried configuring the locator to xpath, cssselector and name etc.
When I try to locate the element directly from the console by id or any other locator, it is located successfully. Just not via the object file.
All information is correct in the object file and in console
Here is object file content:
one=.//*[#id='1']
two=.//*[#id='2']
three=.//*[#id='3']
four=.//*[#id='4']
five=.//*[#id='5']
six=.//*[#id='6']
seven=.//*[#id='7']
eight=.//*[#id='8']
nine=.//*[#id='9']
zero=.//*[#id='0']
equalsto=.//*[#id='equals']
cler=.//*[#id='AC']
result=.//*[#id='Resultbox']
plus=.//*[#id='plus']
minus=.//*[#id='minus']
mul=.//*[#id='multiply']
heading=.//*[#id='Blog1']/div[1]/div/div/div/div[1]/h3
Here is code:
public class CalcTest {
WebDriver driver = new FirefoxDriver();
#BeforeMethod
public void openbrowser() throws IOException, InterruptedException {
driver.manage().window().maximize();
driver.get("http://only-testing-blog.blogspot.in/2014/04/calc.html");
}
/*#AfterMethod
public void closebrowser() {
driver.quit();
}*/
#Test
public void Calc_Operations() throws IOException, InterruptedException {
// Create Object of Properties Class.
Properties obj = new Properties();
// Create Object of FileInputStream Class. Pass file path.
FileInputStream objfile = new FileInputStream(
System.getProperty("user.dir") + "\\src\\Objects\\objects.properties");
// Pass object reference objfile to load method of Properties object.
obj.load(objfile);
// Sum operation on calculator.
// Accessing element locators of all web elements using
// obj.getProperty(key)
String heading = driver.findElement(By.xpath(obj.getProperty("heading"))).getText();
System.out.println(heading);
driver.findElement(By.xpath(obj.getProperty("eight"))).click();
driver.findElement(By.xpath(obj.getProperty("plus"))).click();
driver.findElement(By.xpath(obj.getProperty("four"))).click();
driver.findElement(By.xpath(obj.getProperty("equalsto"))).click();
Thread.sleep(4000);
driver.findElement(By.xpath(("result"))).getAttribute("value");
//String i = driver.findElement(By.name(obj.getProperty("result"))).getAttribute("value");
//System.out.println(i);
//System.out.println(obj.getProperty("eight") + " + " + obj.getProperty("four") + " = " + i);
driver.findElement(By.xpath(obj.getProperty("result"))).clear();
Related
I have 3 pages to navigate page1, page2 and page3. I wanted to capture 3 separate HAR file. i tried the below code when it visit page1 its extracted page one performance data in har file and when i visited page2 its extracted page1 and page2 data but I wanted a separate one please help me to achieve this thanks.
Proxy and driver initialization class file
public class InitializeBrowserDriver {
public WebDriver driver;
public Properties importprop;
public BrowserMobProxy myproxy;
public Har har;
public WebDriver initializer() throws IOException {
importprop=new Properties();
FileInputStream filepath=new
FileInputStream(System.getProperty("user.dir")+"\\src\\main\\java\\datafile.properties");
importprop.load(filepath);
myproxy=new BrowserMobProxyServer();
myproxy.start(0);
Proxy seleniumproxy=new Proxy();
seleniumproxy.setHttpProxy("localhost:" +myproxy.getPort());
seleniumproxy.setSslProxy("localhost:" +myproxy.getPort());
DesiredCapabilities capability=new DesiredCapabilities();
capability.setCapability(CapabilityType.PROXY, seleniumproxy);
capability.acceptInsecureCerts();
capability.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
EnumSet <CaptureType> capturenetworklog=CaptureType.getAllContentCaptureTypes();
capturenetworklog.addAll(CaptureType.getHeaderCaptureTypes());
capturenetworklog.addAll(CaptureType.getRequestCaptureTypes());
capturenetworklog.addAll(CaptureType.getResponseCaptureTypes());
capturenetworklog.addAll(CaptureType.getCookieCaptureTypes());
myproxy.setHarCaptureTypes(capturenetworklog);
myproxy.newHar("newhar");
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.merge(capability);
driver=new ChromeDriver(options);
har=myproxy.getHar();
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
return driver;
}
/*public String urlAccess()
{
String url=importprop.getProperty("baseurl");
return url;
}*/
}
Navigating to different pages using by extending above class:
public class NavigationToPage extends InitializeBrowserDriver {
private void fileWriter() throws IOException {
String title=driver.getTitle();
har=myproxy.getHar();
File myharfile=new File(System.getProperty("user.dir")+"/harfiles/"+title+".har");
har.writeTo(myharfile);
System.out.println(title+ "HAR file has been created successfully");
}
#BeforeClass
public void beforeMethods() throws IOException {
driver=initializer();
driver.get(importprop.getProperty("baseurl"));
driver.manage().window().maximize();
fileWriter();
}
#Test
public void toContactUs() throws IOException {
driver.findElement(By.xpath(importprop.getProperty("toContactUs"))).click();
fileWriter();
}
#Test
public void toSolutions() throws IOException {
WebDriverWait wait=new WebDriverWait(driver, 3);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(importprop.getProperty("toSolutions"))));
this.driver.findElement(By.xpath(importprop.getProperty("toSolutions"))).click();
fileWriter();
}
#AfterClass
public void quitBrowser() throws InterruptedException {
Thread.sleep(1000);
driver.quit();
myproxy.stop();
}
}
Try this solution: https://stackoverflow.com/a/57045946/5966983
For each Test / Navigation (In your case - after performing click ) create new HAR.
myproxy.newHar("page2");
myproxy.newHar("page3");
I have a superclass and my intention is to declare the WebElement in one class as final and use them in the subclass.
Below is the parent class name ObjectRepo where I have declared all the WebElement path. I am trying to use the elements in the subclass, but I see Nullpointer exception at runtime.
The code worked earlier when all the driver.findElements were within the code, but the issue started when I separated it in a different class. I could figure its due to the driver being null, but unable to fix it.
I see the null pointer error on the first line of super class--
final WebElement SignIn_SignUp_button = ObjectRepo.driver.findElement(By.xpath("//button[text()='SIGN IN / SIGN UP']"));
I tried checking if at any point my driver is null and fixing it but didn't work.
Parent class:
public class ObjectRepo {
static WebDriver driver;
public ObjectRepo()
{
// ------------- UIElement of the page ------
final WebElement SignIn_SignUp_button = ObjectRepo.driver.findElement(By.xpath("//button[text()='SIGN IN / SIGN UP']"));
final WebElement EmailField_OnSignIn =ObjectRepo.driver.findElement(By.xpath("//input[#type='email']"));
final WebElement ContinueButton_OnSignIn=ObjectRepo.driver.findElement(By.xpath("//button[text()='Continue']"));
final WebElement PasswordField_OnSignIn=ObjectRepo.driver.findElement(By.xpath("//input[#placeholder='Enter your password']"));
final WebElement ContinueButton_OnPasswordPage=ObjectRepo.driver.findElement(By.xpath("//button[#type='submit' and text()='Continue']"));
//Navbar Elements + MainPage Elements
final WebElement UsernameMenu_Navbar=ObjectRepo.driver.findElement(By.xpath("//div[#class='navbar-link']"));
final WebElement ManagementConsoleMenu_UnderNavbar=ObjectRepo.driver.findElement(By.xpath("//a[#class='navbar-link' and text()='Management Console']"));
final WebElement Logout_UnderNavbar=ObjectRepo.driver.findElement(By.xpath("//a[#class='navbar-link' and text()='Logout']"));
final WebElement StudentMenu=ObjectRepo.driver.findElement(By.xpath("//a[text()='Students']"));
//Management Console Elements
final WebElement DivisionCard_OnManagementConsolePage=ObjectRepo.driver.findElement(By.xpath("//p[text()='Divisions, Classes & Groups']"));
}
My Subclass is as below:
public class CommonActions extends ObjectRepo
{
ObjectRepo obj=new ObjectRepo();
public void LaunchUrl() throws InterruptedException, IOException
{
FileInputStream inputStream = new FileInputStream(syspath + "/InputData/InputData.xlsx");
workbook=new XSSFWorkbook(inputStream);
UserCred=workbook.getSheet("UserCred");
Browser=workbook.getSheet("Browser");
Env=workbook.getSheet("Env");
StudentList=workbook.getSheet("StudentList");
os=System.getProperty("os.name").toLowerCase();
System.out.println(os);
if(driver == null)
{
if(Browser.getRow(0).getCell(1).getStringCellValue().contains("Chrome"))
{
if(os.contains("windows"))
{System.setProperty("webdriver.chrome.driver", syspath + "/Web Driver Exe/chromedriver.exe");}
else
{System.setProperty("webdriver.chrome.driver", syspath + "/Web Driver Exe/chromedriver");}
ChromeOptions options = new ChromeOptions();
options.addArguments("--use-fake-ui-for-media-stream=1");
options.addArguments("--lang=en");
driver = new ChromeDriver(options);
Thread.sleep(1000);
}
else if (Browser.getRow(0).getCell(1).getStringCellValue().contains("Firefox"))
{
if(os.contains("windows"))
{System.setProperty("webdriver.gecko.driver", syspath + "/Web Driver Exe/geckodriver.exe");}
else
{System.setProperty("webdriver.gecko.driver", syspath + "/Web Driver Exe/geckodriver");}
FirefoxOptions ffoptions= new FirefoxOptions();
ffoptions.addPreference("permissions.default.microphone", 1);
ffoptions.addPreference("permissions.default.camera", 1);
ffoptions.addPreference( "intl.accept_languages", "en" );
ffoptions.addPreference("browser.download.folderList", 2);
ffoptions.addPreference("browser.download.dir", "syspath");
ffoptions.addPreference("browser.download.useDownloadDir", true);
ffoptions.addPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");
driver=new FirefoxDriver(ffoptions);
Thread.sleep(1000);
}
else
{
System.out.println("No Env selected");
}
}
//Launch Url
driver.navigate().to(Env.getRow(0).getCell(1).getStringCellValue());
driver.manage().window().maximize();
Thread.sleep(1000);
QuinnActualTitle=driver.getTitle();
}
public void InvokeExcel() throws IOException
{
FileInputStream inputStream = new FileInputStream(syspath + "/InputData/InputData.xlsx");
workbook=new XSSFWorkbook(inputStream);
UserCred=workbook.getSheet("UserCred");
Browser=workbook.getSheet("Browser");
Env=workbook.getSheet("Env");
}
public void CloseExcel() throws IOException
{
FileOutputStream fileOut = new FileOutputStream(syspath + "/InputData/InputData.xlsx");
workbook.write(fileOut);
workbook.close();
fileOut.close();
}
public void Completed()
{
driver.close();
//driver.quit();
}
public void UniversityAdminLogin(int a, int b) throws InterruptedException, IOException
{
SignIn_SignUp_button.click(); //This is where i m trying to use the declared element
Thread.sleep(1000);
EmailField_OnSignIn.sendKeys(UserCred.getRow(a).getCell(b).getStringCellValue());
Thread.sleep(1000);
ContinueButton_OnSignIn.click();
Thread.sleep(2000);
PasswordField_OnSignIn.sendKeys(UserCred.getRow(a).getCell(b+1).getStringCellValue());
ContinueButton_OnPasswordPage.click();
Thread.sleep(2000);
}
But i see the error while instantiating :
[RemoteTestNG] detected TestNG version 7.0.0
org.testng.TestNGException:
Cannot instantiate class SmokeRun.SmokeSuite
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:30)
at org.testng.internal.InstanceCreator.instantiateUsingDefaultConstructor(InstanceCreator.java:193)
at org.testng.internal.InstanceCreator.createInstanceUsingObjectFactory(InstanceCreator.java:113)
at org.testng.internal.InstanceCreator.createInstance(InstanceCreator.java:79)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:114)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:172)
at org.testng.TestClass.getInstances(TestClass.java:102)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:82)
at org.testng.TestClass.init(TestClass.java:74)
at org.testng.TestClass.<init>(TestClass.java:39)
at org.testng.TestRunner.initMethods(TestRunner.java:463)
at org.testng.TestRunner.init(TestRunner.java:342)
at org.testng.TestRunner.init(TestRunner.java:295)
at org.testng.TestRunner.<init>(TestRunner.java:226)
at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:679)
at org.testng.SuiteRunner.init(SuiteRunner.java:196)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:127)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1265)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1093)
at org.testng.TestNG.runSuites(TestNG.java:1032)
at org.testng.TestNG.run(TestNG.java:1000)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23)
... 26 more
Caused by: java.lang.NullPointerException
at Sources.ObjectRepo.<init>(ObjectRepo.java:15)
at Sources.CommonActions.<init>(CommonActions.java:48)
at SmokeRun.SmokeSuite.<init>(SmokeSuite.java:12)
... 31 more
Looking at the code, I think the issue is that when you construct a new ObjectRepo through: ObjectRepo obj=new ObjectRepo(), the driver tries to find those final elements but it fails to do so because the driver initialization is done right below in your subclass. So your assumption that the driver is null is correct from what I see.
What I would suggest is to use a debugger, put a breakpoint exactly at the line that fails and check if the driver is null or not. I believe you are using some sort of IDE, so take a look here: https://www.eclipse.org/community/eclipse_newsletter/2017/june/article1.php if you are using eclipse, or here: https://www.jetbrains.com/help/idea/debugging-code.html if you are using IDEA.
I am trying to save an extent report to my documents folder. I am getting an access is denied problem.
I have tried giving java full control, through all the user groups. I have given jetbrains full control as well but still access is denied.
This is where I am trying to save the file.
public void beforeSuite() {
extent = ExtentManager.createInstance("MobileCustomerCare_" + fileName + ".html");
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(System.getProperty("user.home") + "//Documents/ExtentReport//");
extent.attachReporter(htmlReporter);
I have added the ExtentManager class where the instance gets created
public class ExtentManager {
private static ExtentReports extent;
private static String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date());
static ExtentReports createInstance(String fileName) {
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);
htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setTheme(Theme.DARK);
htmlReporter.config().setDocumentTitle(fileName);
htmlReporter.config().setEncoding("utf-8");
htmlReporter.config().setReportName(fileName);
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
return extent;
}
}
EDIT: I have now changed to use ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(System.getProperty("user.home") + "//Documents/ExtentReport//"); But still getting (access is denied)
I am expecting it to save the file in the location specified. What it's actually doing is saving it in my project.
I think there is no path separator between the folder name and the file name, please try adding a path separator while creating ExtentHtmlReporter Object
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("C:\\Users\\tom.cockram\\Documents\\ExtentReport\\");
Try not to keep spaces in the file name, it will help you in future
public static ExtentReports getInstance() {
if (extent == null)
createInstance("MobileCustomerCare_" + fileName + ".html");
return extent;
}
i had this problem when i was generating an XML file
try this :
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("C://Users//tom.cockram//Documents//ExtentReport");
I am using Webdriver + TestNG. I created a class where you can run any test with the browser of your choice just by using the method below where you enter which browser to run and what test to run as your variables. Now I am trying to feed my "browser" variable and "test" variable from a properties file. When I run it I just get a Pass but nothing happens. If I manually enter the variables it works fine. How come it wont take the values from the properties file? If I do a print - it prints the correct value...
This is the contents of the properties file:
browser="BROWSER GOES HERE"
test="TEST GOES HERE"
Here is my method to run the test:
#Test
public void runTest() throws IOException {
Properties prop = new Properties();
prop.load(new FileInputStream(
"path\\test.properties"));
localBrowser(prop.getProperty("browser"));
masterTest(driver, prop.getProperty("test"));
}
Here is a sandbox example of my test:
If I put the variables in myself the browser will open and execute the test. If I use the property loader it will just print the variables but will not run the test. Here is a sandbox example so you can try it.
Package Structure:
-src/test/java
--SandboxTest
-src/test/resources
--test.properties
Properties File example:
browser=firefox
test=test1
Class example:
public class SandboxTest {
private WebDriver driver;
InputStream input = null;
Properties prop = new Properties();
#Test
public void runTest() throws FileNotFoundException, IOException {
input = new FileInputStream(
(new File(
"C:INSERT_PATH_HERE\\test.properties")));
prop.load(input);
//This doesnt run the test
System.out.println(prop.getProperty("browser"));
System.out.println(prop.getProperty("test"));
localBrowser(prop.getProperty("browser"));
masterTest(driver, prop.getProperty("test"));
/*
* This works
* localBrowser("firefox");
* masterTest(driver, "test1");
*/
}
public void localBrowser(String browser) {
if (browser == "firefox") {
driver = new FirefoxDriver();
} else if (browser == "chrome") {
System.setProperty("webdriver.chrome.driver",
"C:\\INSERT_PATH_HERE\\chromedriver.exe");
driver = new ChromeDriver();
} else if (browser == "ie") {
System.setProperty("webdriver.ie.driver",
"C:\\INSERT_PATH_HERE\\IEDriverServer.exe");
driver = new InternetExplorerDriver(caps);
}
}
public void masterTest(WebDriver driver, String test) {
if (test == "test1") {
Test1(driver);
} else if (test == "test2") {
Test2(driver);
}
}
// *********************************TESTS*****************************************************
public void Test1(WebDriver driver) {
driver.get("http://www.marca.com/en/");
driver.findElement(By.linkText("Barcelona")).click();
}
public void Test2(WebDriver driver) {
driver.get("http://www.marca.com");
driver.findElement(By.linkText("FĂștbol")).click();
}
}
Well.. I am really not sure what you are doing, But if it doesn't work so probably it doesn't load the properties file properly.
try this one
Resource resource = new ClassPathResource("/environment.properties");
properties = PropertiesLoaderUtils.loadProperties(resource);
Tell me if that is ok, hope that helps
EDIT:
The above is part of Spring,
you can use
prop.load(new FileInputStream(new File("test.properties")));
Please make sure that test.properties will be under src folder in the project
EDIT:
The problem not in the property file.. The problem is in the way you are comparing 2 strings
When you do browser == "firefox" The check is for the object address(referenec)
When you are comparing Object type you need to use Eqauls
For example browser.equals("firefox") It will work this way.
Because when you put the value your self it check the refernece and it is the same.. Once you get from property this is 2 diffrent objects but they are "equal" So it will work only with Equals
What you should do instead is put the property in the testing.xml file and then read the property from there instead. Then, if you need to have different property files, you can just create multiple testing.xml files and when you execute the tests just pass the testng.xml file as an argument to TestNG.
In the project I referenced above in the hyperlink, you can see I reference the properties something like so:
#BeforeClass
public void setUp( ITestContext context ) {
suiteParams = context.getSuite().getXmlSuite().getAllParameters();
String browser = suiteParams.get( "browser" );
String sauceUser = suiteParams.get( "sauceUser" );
String sauceKey = suiteParams.get( "sauceKey" )
....
If you have to do it using a properties file, then I would change you code and do it differently. Something like so (assuming use of TestNG):
protected WebDriver driver;
protected String browser;
#BeforeTest
public void setUp() {
Properties prop = new Properties();
prop.load( new FileInputStream("path/test.properties") );
browser = prop.getProperty("browser");
if ( browser.equals("firefox") {
driver = new FirefoxDriver();
} else {
driver = null; // bad choice
}
}
#Test
public void runTest() throws IOException {
driver.navigateTo( url);
// do stuff
Assert.assertTrue( 1 == 1 );
}
#AfterTest
public void cleanUp() {
driver.quit();
}
I want to test a smart GWT app using Selenium.
For this I need to add up
1. user-extensions.js
2. user-extensions-ide.js
in the IDE.
This gives an additional scLocators for locating GWT elements on the page
Now if I want to test the above page using Java, then where will I add these js files in the code
The way I approached this was using the SeleniumServer method, which allowed me to specify my extension file from the java code.
A working SmartGWT example is:
public class Example {
SeleniumServer server = null;
Selenium selenium = null;
HttpCommandProcessor proc = null;
#Before
public void setUp() throws Exception {
RemoteControlConfiguration config = new RemoteControlConfiguration();
config.setUserExtensions(new File("bin/selenium/user-extensions.js"));
server = new SeleniumServer(config);
server.boot();
proc = new HttpCommandProcessor("localhost",
4444,
"*firefox",
"http://test");
selenium = new DefaultSelenium(
proc);
selenium.start();
selenium.open("http://test");
}
#Test
public String justATest() {
execCommand("waitForElementClickable", "scLocator=//Window[ID=\"errorWindow\"]/item[0][Class=\"Label\"]/");
String elementTest = selenium.getText("scLocator=//Window[ID=\"errorWindow\"]/item[0][Class=\"Label\"]/");
assertEquals(elementTest, "lorem");
}
protected String execCommand(String command, String locator) {
String[] locatorArg = {locator};
return proc.doCommand(command, locatorArg);
}
#After
public void stopSelenium() {
System.out.println("in after hook");
if (selenium != null) {
selenium.deleteAllVisibleCookies();
selenium.stop();
}
if (server != null) {
server.stop();
}
System.out.println("after after hook");
}
}