Accessing method from one class to another class in same package - java

I have two classes called AdminLogin and CreateCustomer in the same package 'testing. I written a method commonLogin() in AdminLogin.java for LOGIN purpose.So i need to call the commonLogin() in CreateCustomer class,instead of writing the same login code.How can i do this? Any one please tell me.
AdminLogin.java:
package testing;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AdminLogin {
public static void commonLogin()
{
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit BluBilling
driver.get("http://testing.blubilling.in");
//Fetching the username and password
WebElement element = driver.findElement(By.id("j_username"));
// Enter something to search for
element.sendKeys("xxxx");
WebElement element1 = driver.findElement(By.id("j_password"));
element1.sendKeys("zzzz");
// Entering into bluBilling application
element.submit();
}
public static void main(String[] args) {
AdminLogin .commonLogin();
}
}
and CreateCustomer.java
package testing;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CreateCustomer {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit BluBilling
driver.get("http://testing.blubilling.in");
//Fetching the username and password
WebElement element = driver.findElement(By.id("j_username"));
// Enter something to search for
element.sendKeys("xxxx");
WebElement element1 = driver.findElement(By.id("j_password"));
element1.sendKeys("zzzz");
// Entering into bluBilling application
element.submit();
//Creating a Customer
driver.navigate().to("https://testing.blubilling.in/customer /createCustomer2");
//Entering the details of the Customer
WebElement element4 = driver.findElement(By.id("name"));
element4.sendKeys("");
WebElement element5 = driver.findElement(By.id("firstName"));
element5.sendKeys("chris");
WebElement element6 = driver.findElement(By.id("lastName"));
element6.sendKeys("broad");
WebElement element7 = driver.findElement(By.id("emailPrimary"));
element7.sendKeys("chris#blusyn.com");
WebElement element8 = driver.findElement(By.id("username"));
element8.sendKeys("");
WebElement element9 = driver.findElement(By.id("password"));
element9.sendKeys("Chris1234");
WebElement element10 = driver.findElement(By.id("confirm"));
element10.sendKeys("Chris1234");
driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS);
//Saving the Customer details
WebElement element11 = driver.findElement(By.cssSelector("input[value='Save']"));
element11.click();
}
}

In CreateCustomer:
AdminLogin.commonLogin();
is what you need.
But, I would recommend that CreateCustomer and AdminLogin be 2 separate functions in one single class, say Admin, since they are the actions that an admin could perform (if I understand your task right).
Goodluck.

Related

Selenium non-deterministic loop over List<WebElement>

I am trying to do some quick tests to learn selenium for web scraping purposes. I am trying to loop over the menu items of the taco bell website. What I find confusing is that the first element of the List, isn't what is selected by the first or second click. The actual selection is usually the 2nd or 3rd element. It is non-deterministic. What am I doing wrong?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class Main {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/Applications/chromedriver");
driver = new ChromeDriver();
driver.get("https://www.tacobell.com/food");
List<WebElement> listOfMenuCategories = driver.findElements(By.cssSelector(".cls-category-card-item-card"));
for(WebElement webElement : listOfMenuCategories){
scanTacoBellMenuCategory(webElement);
}
System.out.println("1: "+listOfMenuCategories.size());
driver.quit();
}
public static void scanTacoBellMenuCategory(WebElement webElement){
webElement.click();
List<WebElement> listOfSubMenuCategories = driver.findElements(By.cssSelector(".product-item"));
for(WebElement submenuCategory : listOfSubMenuCategories){
scanTacoBellSubMenuCategory(submenuCategory);
}
}
public static void scanTacoBellSubMenuCategory(WebElement webElement){
webElement.click();
}
}
Thanks.
UPDATE-------------------------------
I now realize that my example was unnecessarily complicated and intent was not obvious. Here is a more direct example:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class MainTwo {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/Applications/chromedriver");
driver = new ChromeDriver();
driver.get("https://www.tacobell.com/food");
List<WebElement> listOfMenuCategories = driver.findElements(By.cssSelector(".cls-category-card-item-card"));
for(WebElement webElement : listOfMenuCategories){
webElement.click();
break;
}
driver.quit();
}
}
The tacobell menu (https://www.tacobell.com/food) has 16 categories in the following order: New, Favorites, Combos, Specialties, Tacos, Burritos, Quesadillas, Nachos, Value Menu, Sweets, Sides, Drinks, Power Menu, Party, Packs, Vegetarian, Breakfast.
When I loop over these items, I "click" the first one in the forEach loop. I would expect that to be the "New" category. I would also expect the result to be repeatable. However, neither of those statements are true. It usually opens one of the "Favorites", "Combos", or "Specialties" menus. However, it can truly open pretty much anything.
It seems as if the Webdriver is non-blocking in some way. In particular, the webElement.click() event doesn't seem to stop the execution of the forEach loop. It is almost as if the the webElement was in another thread.
Why doesn't the "New" menu get displayed when I run the above code and why isn't this deterministic?

Need to input value into field, check result, and then add one character in Selenium Java

This is the first time I have ever coded, so excuse my ignorance.
I have the following Selenium code that is for online ordering from a restaurant. At the end, it is putting a value into a field, checking and printing the result, then what I need to do is change the original input, and do it again. So I need a loop. I think.
package Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import java.util.Random;
import java.util.Scanner;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\testuser\\Desktop\\Eclipse\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Actions action = new Actions(driver);
driver.get("onlineorder.com");
//driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
WebElement startorder = driver.findElement(By.cssSelector("#startOrder_148"));
startorder.click();
Thread.sleep(4500);
action.sendKeys(Keys.RETURN);
Thread.sleep(3000);
WebElement selectfood = driver.findElement(By.xpath("//a[contains(text(),\"NEW! Steak Po'boy (670 cal)\")]"));
selectfood.click();
Thread.sleep(4000);
WebElement additem = driver.findElement(By.cssSelector("#addItemToOrder"));
additem.click();
Thread.sleep(5000);
WebElement checkout = driver.findElement(By.xpath("//a[contains(text(),'Checkout')]"));
checkout.click();
Thread.sleep(5000);
WebElement loginbutton = driver.findElement(By.cssSelector("#logonCheckoutBtn"));
loginbutton.click();
Thread.sleep(3500);
//WebElement click5 = driver.findElement(By.xpath("//input[#id='email']"));
//action.click(click5).perform();
driver.findElement(By.xpath("//input[#id='email']")).sendKeys("email#gmail.com");
driver.findElement(By.cssSelector("#loginPassword")).sendKeys("password");
WebElement loginbutton2 = driver.findElement(By.cssSelector("#loginButton"));
loginbutton2.click();
Thread.sleep(5500);
WebElement paymenttype = driver.findElement(By.id("selectPaymentType"));
Select payment=new Select(paymenttype);
payment.selectByIndex(3);
driver.findElement(By.id("numberchecker")).sendKeys("1000");
WebElement checkbalance = driver.findElement(By.xpath("//div[#class='clearfix ng-scope']//div[#class='clearfix']//a[#class='numberchecker btn'][contains(text(),'Check balance')]"));
checkbalance.click();
Thread.sleep(1500);
for(WebElement link:driver.findElements(By.xpath("//span[#class='popup_message ng-binding']")))
{
System.out.println(link.getText());
}
WebElement okaybalance = driver.findElement(By.cssSelector("#btnPopupOk"));
okaybalance.click();
Everything works fine until here. What I need to do is go back and change the original input value (1000) by 1 to 1001. The error I get for getInputNumber is "AnnotationName expected after this token". Also, the name of the class "addnumber" gives error "Illegal modifier, only abstract or final is permitted". "class" gives error "syntax error, # expected".
public class addnumber() {
private static float inputNumber= 1000f;
public static float getInputNumber() {
return inputNumber+ 1.0f;
}
}
Thread.sleep(4000);
//driver.quit();
}
}
You have declared class wrongly. It should be
public class Sample{
//Then the stuff you want to do.
}
To access this class you need to create outer class object and them inner
Like this.
Outer d=new Outer();
d.Sample obj=new d.Sample();
But as you have declared member static for sample class you can just call them using classname.

Trying to select forum threads with new posts

Hello everyone I'm learning Selenium with Java. How can I select forum threads with new posts (posts the user has not already posted in).
Example of my assignment:
Given the list of threads at: http://siteownersforums.com/forumdisplay.php?s=ea4353212d6e4bcb50abb3f5790fa97a&f=3
Open each new thread that you haven't posted in. Hint: You'll want to
select the elements first, and then use the .click() method.
and this is what I have so far:
package main;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test {
public static void main(String[] args) {
//Set the Driver and the Webpage.
System.setProperty("webdriver.chrome.driver", "./chromedriver");
WebDriver chromeDriver = new ChromeDriver();
chromeDriver.get("http://siteownersforums.com/forumdisplay.php?s=ea4353212d6e4bcb50abb3f5790fa97a&f=3");
//Confirmation
System.out.println("Successfully Opened up: "+ chromeDriver.getTitle());
//Select each of the threads and click it.
for (int i =0; i< 27; i++){
WebElement tempThread = chromeDriver.findElement(By.cssSelector(""));
tempThread.click();
}
}//Closes method test
}//Closes class

How we click same class name ON button for different field name in selenium webdriver

How we click same class name for ON / OFF button for different field name in selenium webdriver
(eg)
1) Email Notification - one element
2) System fees - second element
3) Birthdate - third element
these are have same class name - "toggle-group".How we click these three button.
How we write click button action for this
Not like checkbox option
As you can see in this very helpful article, you can use many methods:
driver.findElement(By.id("element id"))
driver.findElement(By.className("element class"))
driver.findElement(By.name("element name"))
driver.findElement(By.tagName("element html tag name"))
driver.findElement(By.cssSelector("css selector"))
driver.findElement(By.link("link text"))
driver.findElement(By.xpath("xpath expression"))
You can find the elements by their text
driver.findElement(By.linkText("first")).click();
Or
driver.findElement(By.partialLinkText("first")).click();
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class check {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
List<WebElement> we = new ArrayList<WebElement>();
we = driver.findElements(By.name("chk"));
we.get(0).click(); // clicks on "first"
we.get(1).click(); // clicks on "second"
we.get(2).click(); // clicks on "third"
}
}
/* another option */
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class check {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
List<WebElement> we = new ArrayList<WebElement>();
we = driver.findElements(By.name("chk"));
for(WebElement check: we)
{
check.click(); // click all the 3 elements and comes out of loop
}
}
}
Hope this helps..

Selenium cannot find element on website (chrome/Java)

Trying to test/learn selenium to login
the error - Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible
package com.indeed.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class test1 {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\****\\Desktop\\neww\\trainingfiles\\chromedriver.exe.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.neopets.com/login/index.phtml");
driver.findElement(By.name("username")).sendKeys("test1");
}
private static void sleep(int i) {
}
}
I had a look at that web page. The problem is that there are two input fields with the name "username". One of them is not visible. Probably Selenium is getting that one. What you should do is:
List<WebElement> elements = driver.findElements(...);
and then get the second one (or the first, whatever), then try:
elements.get(1).sendKeys(...);

Categories