Install Selenium in Ubuntu 16.04 and check it using java code - java

I want to make a java program(GUI) which will auto-fill the username and password in a website on click of a button. I am new to networking concepts in java. A friend told to use selenium because that will make it easy but I am not able to use it successfully (tried code is shown below). I am using Ubuntu 16.04 OS and using gedit to write the source code. Any help would be highly appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class pextax implements ActionListener {
JFrame frame;
JButton button;
.
.
.
public void GUI() throws Exception {
button = new JButton("Click to open PEXTAX official website");
button.addActionListener(this);
.
.
.
}
public void actionPerformed (ActionEvent event) {
if(event.getSource()==button) {
try {
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("https://www.pextax.com");
driver.manage().window().maximize();
WebElement username = driver.findElement(By.name("username"));
WebElement password = driver.findElement(By.name("password"));
username.sendKeys("123456789");
password.sendKeys("1234");
}catch(Exception e) { }
}
}
}
vivek#vivek-HP-Notebook:~$ javac pextax.java
pextax.java:4: error: package org.openqa.selenium does not exist
import org.openqa.selenium.By;
^
pextax.java:5: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebDriver;
^
pextax.java:6: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebElement;
^
pextax.java:7: error: package org.openqa.selenium.firefox does not exist
import org.openqa.selenium.firefox.FirefoxDriver;
^
pextax.java:8: error: package org.openqa.selenium.support.ui does not exist
import org.openqa.selenium.support.ui.ExpectedCondition;
^
pextax.java:9: error: package org.openqa.selenium.support.ui does not exist
import org.openqa.selenium.support.ui.WebDriverWait;

Related

Android app closes after .click() command

I'm trying to automate a app to do everything that you would have to do by hand. My main goal right now is to get it to click a button after logging into the app.
This is the very last line of code in my IDE
driver.findElement(By.id("com.offerup:id/main_text")).click();
After this line of code executes, OfferUp, the app that I'm testing on, closes. There are no failures in console but, I don't want it to close after that line of code executes.
When I log into the app without running my code, the app stays open but, when I run my code, it closes after driver.findElement(By.id("com.offerup:id/main_text")).click(); is executed.
Why this is happening?
Here is my full code -
package OpenOfferUpTest;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.server.handler.FindElement;
import org.testng.annotations.Test;
import org.testng.annotations.*;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
public class OpenOfferUp {
AndroidDriver driver;
#Test
public void OpensOfferUp() throws MalformedURLException
{
File OfferUp = new File("C:\\Users\\boung\\Desktop\\OfferUp.apk");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "Virtual Device");
cap.setCapability("platformName", "android");
cap.setCapability("null", "OfferUp");
cap.setCapability("appPackage", "com.offerup");
cap.setCapability("appActivity", "com.offerup.android.login.splash.LoginSplashActivity");
driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), cap);
}
#Test
public void SimpleTest() throws InterruptedException {
driver.findElement(By.id("com.offerup:id/email_button")).click();
By path = By.xpath("//*[#text='Enter your email address']");
driver.findElement(path).sendKeys("sourgta#gmail.com");
driver.findElement(By.id("com.offerup:id/next_button")).click();
By path1 = By.xpath("//*[#text='']");
driver.findElement(path1).sendKeys("12manytimes");
driver.findElement(By.id("com.offerup:id/main_text")).click();
}
}

"The attribute value is undefined for the annotation type Parameters" error is displayed for Cross-Browser Testing Script

I am trying this cross-browser testing using Selenium.
CrossBrowser.java:
package automationFramewok;
import java.net.MalformedURLException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.beust.jcommander.Parameters;
// I am getting the following error on the next line
//
// "The attribute value is undefined for the annotation type Parameters"
//
#Parameters({"browser"})
public class CrossBrowser {
#SuppressWarnings("deprecation")
#BeforeTest
public void setUp(String browser) throws MalformedURLException {
if (browser.equalsIgnoreCase("Firefox")) {
System.out.println("Running Firefox");
System.setProperty("webdriver.gecko.driver","E:\\\\Selenium-required files\\geckodriver\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("chrome")) {
System.out.println("Running Chrome");
System.setProperty("webdriver.chrome.driver", "E:\\\\\\\\Selenium-required files\\\\chromedriver\\\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("opera")) {
System.out.println("Running Opera");
// driver = new OperaDriver(); --Use this if the location is set properly--
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("opera.binary", "C://Program Files (x86)//Opera//opera.exe");
capabilities.setCapability("opera.log.level", "CONFIG");
System.setProperty("webdriver.opera.driver", "E:\\\\\\\\Selenium-required files\\\\operadriver\\\\operadriver.exe");
OperaDriver driver = new OperaDriver(capabilities);
}
}
}
I am receiving the following error message:
The attribute value is undefined for the annotation type Parameters
How can I resolve this?
Check out your list of import statements. I think you want
import org.testng.annotations.Parameters;
and not
import com.beust.jcommander.Parameters;
The same issue I was facing and problem was with import statement. I was using the following import statement.
import org.junit.runners.Parameterized.Parameters;
Replaced with the following import statement and issue got resolved.
import org.testng.annotations.Parameters;

I have issue while automating Odoo application

i am automating Odoo pos application. but while adding product to the cart, i got error like "Element is not clickable at point (659,166)". i have already created 1 item and trying to add that in cart but element is not found. need help.
package odoo1;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class odoo {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
// for Log in functionality
driver.get("https://www.odoo.com/trial?selected_app=point_of_sale");
driver.findElement(By.id("username")).sendKeys("mark");
driver.findElement(By.id("email")).sendKeys("mark#gmail.com");
driver.findElement(By.id("company-name")).sendKeys("odooo");
driver.findElement(By.id("phone")).sendKeys("561234897");
driver.findElement(By.id("country-id")).sendKeys("India");
driver.findElement(By.name("lang")).sendKeys("English");
Select sell=new Select(driver.findElement(By.name("company_size")));
sell.selectByIndex(2);;
Select sell1=new Select(driver.findElement(By.id("plan")));
sell1.selectByIndex(2);
driver.findElement(By.xpath(".//*[#id='wrapwrap']/main/div/div/div[2]/div[2]/div/form/input[8]")).click();
// for creating product
driver.findElement(By.xpath("html/body/div[3]/div[3]/div[1]/a[2]/div[2]")).click();
driver.findElement(By.xpath("html/body/nav/div/ul[1]/li[2]/a")).click();
driver.findElement(By.xpath("html/body/nav/div/ul[1]/li[2]/ul/li[3]/a/span")).click();
driver.findElement(By.xpath("html/body/div[3]/div[2]/div[1]/div/button")).click();
driver.findElement(By.xpath(".//*[#id='o_field_input_4']")).sendKeys("iphone");
driver.findElement(By.xpath(".//*[#id='o_field_input_4']")).click();
driver.findElement(By.xpath(".//*[#id='o_field_input_13']")).sendKeys("abcd");
driver.findElement(By.xpath(".//*[#id='o_field_input_14']")).sendKeys("123456");
driver.findElement(By.xpath(".//*[#id='o_field_input_15']"));
driver.findElement(By.xpath(".//*[#id='o_field_input_16']")).clear();
driver.findElement(By.xpath(".//*[#id='o_field_input_16']")).sendKeys("25000");
driver.findElement(By.xpath(".//*[#id='o_field_input_20']")).clear();
driver.findElement(By.xpath(".//*[#id='o_field_input_20']")).sendKeys("25000");
Thread.sleep(1000);
driver.findElement(By.xpath("html/body/div[3]/div[2]/div[1]/div/div[2]/button[1]")).click();
Thread.sleep(900);
driver.findElement(By.xpath("html/body/nav/div/ul[1]/li[1]/a/span")).click();
driver.findElement(By.xpath("html/body/div[4]/div/div/div[1]/div[2]/div/div[1]/button")).click();
Thread.sleep(500);
driver.findElement(By.xpath("html/body/div[1]/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[2]/table/tbody/tr[2]/td/div/div/div/div/span[2]/div[1]")).click();
Thread.sleep(700);
driver.findElement(By.xpath("//button")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("html/body/div[1]/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[2]/table/tbody/tr[2]/td/div/div/div/div/span[2]/div[1]/img")).click();
}
}
By looking at the exception, it looks like the right path is not selected.
I have just checked the website which you are trying to automate.
Steps I have followed:
Click on Open Cart tab.
Select Shipping option.
Click on Add to Cart for the product which you want to add in your path.
Xpath Used for Add To Cart button:
//h3[normalize-space()="Opencart Marketplace"]/../div[3]//button
I have selected the product "Opencart Marketplace". You can select any product you want. Just replace Opencart Marketplace with the product which you want to select.
Code:
System.setProperty("webdriver.gecko.driver", "src/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://store.webkul.com/OpenCart-Modules.html");
Thread.sleep(2);
driver.findElement(By.xpath("//h3[normalize-space()='Opencart Marketplace']/../div[3]//button")).click();
driver.findElement(By.xpath("//div[#class='product_cart_container']/button")).click();

"cannot instantiate the type select" while selecting an item from drop down

package Saradhi;
import java.util.concurrent.TimeUnit;
import org.apache.bcel.generic.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoginAndNavigate {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "E:ChromeDriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://opensource.demo.orangehrmlive.com/");
WebElement frmtime = driver.findElement(By.id("workShift_workHours_from"));
Select se = new Select(frmtime); // error line
I'm getting an error message in the above line.
I have tried importing related packages but still didn't work.
You are importing a wrong package. Try
import org.openqa.selenium.support.ui.Select;
instead of
import org.apache.bcel.generic.Select;
Use this import:
import org.openqa.selenium.support.ui.Select;
And remove this one:
import org.apache.bcel.generic.Select;

How to import select class in webdriver?

I tried importing select class in my program using selenium webdriver,
but I am not able to import the predefined package.
Can anyone please guide me on this?
package com.siri.dev;
import org.apache.bcel.generic.Select;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class MyntraTests {
private WebDriver driver;
#Before
public void setup() {
intializedriver("firefox");
}
#Test
public void get() {
driver.get("http://www.myntra.com");
driver=waitForPageLoaded(driver);
driver.manage().window().maximize();
System.out.println("Page opened successfully");
WebElement element = driver.findElement(By.className("tab"));
org.openqa.selenium.support.ui.Select elem = new Select(element);
elem.selectByVisibleText("BIBA");
}
private void intializedriver(String browser) {
// TODO Auto-generated method stub
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
import org.openqa.selenium.support.ui.Select;
Use
Select selectElement = new Select(driver.findElement(By.cssSelector("")));
The reason you are having this
org.openqa.selenium.support.ui.Select elem = new Select(element);
is cause you have imported a wrong class imported already
import org.apache.bcel.generic.Select;
remove that import by deleting it and then
import org.openqa.selenium.support.ui.Select
If you are using Eclipse you can always remove unused imports by ctrl+shift+o.
Select is a class of package org.openqa.selenium.support.ui
So you are supposed to do a import statement as follows:
import org.openqa.selenium.support.ui.Select;
and then you can do your task as:
Select elem = new Select(element);
elem.selectByVisibleText("BIBA");
U have imported a wrong package : import org.apache.bcel.generic.Select;
If you use Maven, you will want to know that packages under org.openqa.selenium.support are in artifact selenium-support. That does not get pulled in along with selenium-api or any of the selenium-*-driver artifacts. You can refer to the Selenium Maven information.

Categories

Resources