im trying to make test in Eclipse, using Selenium and Firefox webdriver. I wrote next class:
public class Selenium {
FirefoxDriver driver;
#BeforeTest
public void setup(){
driver = new FirefoxDriver();
}
#Test
public void go_to(String url){
driver.get(url);
}
Now in my main class im trying do that:
String url = JOptionPane.showInputDialog("Please input where do you want to go");
selenium.setup();
report.start_report();
selenium.go_to(url);
But also, i want to write
if(selenium.driver.getStatus)
Is there any command, to take boolean value, from my driver.get(url)? For example, if successful go to web page, then true, and if error, then false?
Related
I need help with creating Java automation with Selenium Webdriver to Pom or Pomm factory.
I've read how to create pom without any success.
Please help.
I need help on how to create java automation in pom. strong textHow to conver it?
String baseUrl = "https:amazon.com/";
WebDriver driver;
NavigationPom navigationPom;
private final boolean useFirefoxbrowser = false;
#BeforeClass
public static void setupClass() {
WebDriverManager.chromedriver().setup();
WebDriverManager.firefoxdriver().setup();
}
#Before
public void setUp() {
if (useFirefoxbrowser == false) {
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.addArguments("--width=1240", "--height=720");
driver = new FirefoxDriver(firefoxOptions);
} else {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--window-size=1920,1080");
driver = new ChromeDriver(chromeOptions);
}
}
#Test
public void MacbookTest1() {
driver.get(baseUrl);
driver.manage().window().maximize();
driver.findElement(By.xpath("//input[#id='twotabsearchtextbox']")).click();
driver.findElement(By.xpath("//input[#id='twotabsearchtextbox']")).sendKeys("macbook");
driver.findElement(By.xpath("//input[#id='nav-search-submit-button']")).click();
driver.findElement(By.xpath("//input[#id='nav-search-submit-button']")).click();
driver.findElement(By.xpath("//li[#id='p_89/Lenovo']/span/a/div/label/i")).click();
//Checkboxes
boolean enabled = driver.findElement(By.xpath("//li[#id='p_89/Lenovo']/span/a/div/label/i")).isEnabled();
Start from creating a simple implementation:
How the Page class might look:
class Page {
private WebDriver driver;
public Page(WebDriver driver) {
this.driver.driver = driver;
}
//Define locators
//Keep elements as By on the top of the class
private By someElement1 = By.xpath("...");
private By someElement2 = By.xpath("...");
//Define page methods
// Examples:
// public void clickSomeElement()
// public void fillSomeInput()
public void doSomeAction() {
WebElement element = driver.findElement(someElement1);
// do something with the element
// optionaly wait for something after action
}
// Examples:
// public boolean isPageLoaded()
// public boolean isSomeElementDisplayed()
// public String getSomeElementText()
// public int getSomeElementsCount()
public Object getSomeData() {
WebElement element = driver.findElement(someElement2);
// do something with the element and return
}
}
Use pages in your tests, do not work with WebDriver directly.
#Test
public void someTest() {
Page page = new Page(driver);
page.doStep1();
page.doStep2();
assertEquals(page.getSomeData(), "Some expected result", "Optional Error message");
}
And..
Start from creating a test scenario code, create test steps, which you like to have (methods, which will be underlined in IDE as non-existing), and think about the data, which you like to check.
Then just implement all the non-existing methods you need in Page classes.
Maybe it sounds complicated, but try to start from simple test scenarios and it should become more clear after some practice.
I had the same issue. If this is a Java project, with Eclipse and Intellij IDEA, you can right click the project and select Convert to Maven. From there, it's fine adjustments (versions) in the POM file.
I have the following test suite to run an automation script to login to gmail then another script to click the Compose button:
TestSuite.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Web Admin Tests" parallel="false">
<test name="BOS - Account Class">
<classes>
<class name="secondtestngpackage.GmailComposeEmail" />
<class name="secondtestngpackage.GmailLogin" />
</classes>
</test>
</suite>
When I run this test suite, the first test does not get passed the login screen in gmail, but the second test does, even though they reference the same functions. Is there a reason why this would happen? One test is able to enter the user id and password, since it is the second/last test, but when the first test is run, it is like the second test interferes with it, since its browser is now in focus.
GmailLogin.java:
#BeforeTest
public void launchBrowser() {
ReuseFunctions.OpenApp("Chrome", "https://gmail.com");
}
//Test 1: Log into Gmail
#Test(priority=1)
public void LoginToGmailAccount() {
**GmailComposeEmail.java**
#BeforeTest
public void launchBrowser() {
ReuseFunctions.OpenApp("Chrome", "https://gmail.com");
}
#Test(priority=2)
public void LoginToGmailAccount() {
Reusable Functions File:
ReuseFunctions.func_EnterCredentials("username", "password");
public class ReuseFunctions {
public static WebDriver driver;
/*Function 1: Select Browser and Open Application Function
*/
public static Object OpenApp (String Browser, String URL) {
//Receive Browser Name Function
Func_LaunchBrowser(Browser);
//Receive Website and Open Function
func_OpenURL(URL);
return driver;
}
//Select Browser
public static WebDriver Func_LaunchBrowser(String Browser) {
String driverPath = "C:/EclipseJavaDev/";
if(Browser=="Chrome"){
System.out.println("launching chrome browser");
System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
driver = new ChromeDriver();
}else if(Browser=="FF"){
driver= new FirefoxDriver();
}else if(Browser=="IE"){
System.setProperty("webdriver.ie.driver", "Drivers\\IEDriverServer.exe");
driver= new InternetExplorerDriver();
}
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
return driver;
}
//Open URL
public static void func_OpenURL(String URL){
driver.get(URL);
driver.manage().window().maximize();
}
The problem lies with where your WebDriver instance is instantiated. Think of each instance of WebDriver as a unique browser session. So the safest way to ensure each test gets its own driver instance (browser session), is to initialize it at the Test class, not the common helper class.
GmailLogin:
// Initialize Webdriver and then asks your common function to return it
private WebDriver driver;
#BeforeTest
public void launchBrowser() {
driver = ReuseFunctions.OpenApp("Chrome", "https://gmail.com");
}
#Test(priority=1)
public void LoginToGmailAccount() {
// pass driver as a method argument to other methods
}
//Don't forget to close the browser
#AfterTest
public void quitBrowser() {
driver.quit();
}
Refactor your other Test class the same away as above. (Or reconsider if you really need another class. You could just add another method into the same class and use priority and/or dependsOnMethods attributes to control the execution order. But that is a different topic and out of the scope of this question :) ).
ReuseFunctions:
// Remove the global Webdriver instantiation
//Change return type from Object to WebDriver
public static WebDriver OpenApp (String Browser, String URL) {
WebDriver driver = Func_LaunchBrowser(Browser);
func_OpenURL(URL, driver); //pass in the driver as arg
return driver;
}
//Refactor openUrl method to use the driver from arg
public static void func_OpenURL(String URL, WebDriver driver){...}
This should work. Let me know how it goes.
I need a little help. I'm trying to run an automated test on the website http://zara.com and i want to select the language from the language dropdown.
This is the HTML code from Zara. https://prntscr.com/g6hdiv
This is the code i've tried with Selenium 2.53 in IntelliJ
public class RegistrationTest {
WebDriver driver;
#Before
public void setUp(){
driver = new FirefoxDriver();
driver.get("http://zara.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#After
public void tearDown(){
driver.quit();
}
#Test
public void test(){
WebElement languageDropdown = driver.findElement(By.id("language"));
Select selectLanguage = new Select(languageDropdown);
selectLanguage.selectByValue("en");
}
}
I always receive the error below even if I've tried in different setups but it didn't work.
org.openqa.selenium.ElementNotVisibleException: The element is not currently visible and so may not be interacted with
Could you please tell me what am I doing wrong?
Appreciate the help.
The element is not currently visible and so may not be interacted with
You need to scroll the page, so that the element is in the current viewport. Something like this:
WebElement languageDropdown = driver.findElement(By.id("language"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", languageDropdown);
Select selectLanguage = new Select(languageDropdown);
selectLanguage.selectByValue("en");
I don't know why I am getting 2 firefox browsers opened for the follwoing example. Can some one please tell me what is wrong in below code. I am new to cucumber and I am trying to develop cucumber poc with page object model.
Feature file:
Scenario: Smoke test for application
Given I am on home page
Step Defination file:
public class HomePageSteps {
CustomerDetails customerDetails;
HomePage homePage=new HomePage();
public HomePageSteps(CustomerDetails customerDetails){
this.customerDetails=customerDetails;
}
#Before
public void environmentSteup(){
homePage.envSetup();
}
#Given("^I am on home page$")
public void i_am_on_home_page() throws Throwable {
homePage.openURL();
}
}
Actual implementation of Step definition file:(HomePage.java)
public class HomePage extends BasePage{
public void openURL() {
driver.get("https://applicationURL.aspx");
System.out.println("I am on home page executed");
}
public void envSetup() {
driver=new FirefoxDriver();
driver.manage().window().maximize();
}
}
BasePage.java
public abstract class BasePage {
protected WebDriver driver=new FirefoxDriver();
}
CustomerDetails.java
public class CustomerDetails {
private String mdn=null;
private String Fname=null;
private String Lname=null;
public String getMdn() {
return mdn;
}
public void setMdn(String mdn) {
this.mdn = mdn;
}
}
2 firefox browsers are opened:
First it opens a blank browser. Later it opens another browser and in this browser it opens the application URL.
You have two calls to open browser windows...
Once in the sub-class in envSetup() - driver=new FirefoxDriver();
And in the super class driver variable declaration with initialization - protected WebDriver driver=new FirefoxDriver();
You have to remove one of them, no need for the super class one... This is the one giving you the blank window
Refer to this page. Your maximize() call in envSetup() might be doing more than you think
In selenium webdriver what is manage() [driver.manage()]
edit:
You also do not need to instantiate a new FirefoxDriver() outside of BasePage as you have already instantiated a driver field with that object. Anything extending BasePage will have access to that driver field. It is not a problem that you're doing this, it is just extraneous code that doesn't need to be there
I'm running a code on webdriver with TESTNG... the first test works perfectly fine but after when I try executing test2 ... the driver.findelement gets underlined in red and doesn't execute at all. Previous driver.findelement was brown but after test2 is blue, any reason to why its not working?
#Test(priority=1)
public void launchSandBoxTestingTestNG() throws InterruptedException{
// Import FireFox Driver
WebDriver driver = new FirefoxDriver();
// Open up Sandbox Page
driver.get("****");
// Enter Usename and Password
// User
driver.findElement(By.id("userId")).sendKeys("****");
Thread.sleep(3000);
// Password
driver.findElement(By.id("password")).sendKeys("****");
Thread.sleep(3000);
// Click Login Button
driver.findElement(By.id("loginButton")).click();
}
#Test(priority=2)
public void test2(){
driver.findElement(By.xpath("****")).click();
// When I try running this code above it underlines the find element in red
// When I run it on web driver the test2 syntax doesnt work
//It gives me an option of casting an argument but not sure what that means
}
}
The question is not very clear may be this might be the problem.
You are creating an WebDriver object inside a function.
Make WebDriver object global.
Example
public class test {
WebDriver driver = new FirefoxDriver();
public void test1(){
//test logic
}
public void test2(){
// test logic
}
}
I would also put "#Test(priority=1)" inside the "public void launchSandBoxTestingTestNg" and declare the webdriver inside the "launchSandBoxTestingTestNG" but outside the test methods
public void launchSandBoxTestingTestNG() throws InterruptedException{
// Import FireFox Driver
WebDriver driver = new FirefoxDriver();
#Test(priority=1)
public void test1(){
// Open up Sandbox Page
driver.get("****");
// Enter Usename and Password
// User
driver.findElement(By.id("userId")).sendKeys("****");
Thread.sleep(3000);
// Password
driver.findElement(By.id("password")).sendKeys("****");
Thread.sleep(3000);
// Click Login Button
driver.findElement(By.id("loginButton")).click();
}
#Test(priority=2)
public void test2(){
driver.findElement(By.xpath("****")).click();
// When I try running this code above it underlines the find element in red
// When I run it on web driver the test2 syntax doesnt work
//It gives me an option of casting an argument but not sure what that means
}
}