0
votes

I have one class as below:

public class Module3 {

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
findElement1 a = new findElement1();
driver.get("webAddress");
driver.findElement(By.xpath("//*[@id='Username']")).sendKeys("A1");
driver.findElement(By.xpath("//*[@id='Password']")).sendKeys("1");
driver.findElement(By.xpath("//*[@id='loginBox']/form/p/button")).click();
Thread.sleep(2000L);
driver.findElement(By.xpath("//*[@id='mainNav']/li[2]/a")).click();
Thread.sleep(2000L);
driver.findElement(By.xpath("//*[@id='addNewEntryButton']")).click();


WebElement dropdown = driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[1]/select"));
List<WebElement> dropOptions = dropdown.findElements(By.tagName("Option"));
for (int i=0; i<dropOptions.size(); i++)
{
 System.out.println(dropOptions.get(i).getText());
}
Thread.sleep(2000L);
driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[1]/select")).sendKeys(Keys.ARROW_DOWN);
driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[5]/input")).sendKeys("5");
driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[6]/input")).sendKeys("5");
driver.findElement(By.xpath("//*[@id='saveEntryButton']")).click();

Thread.sleep(5000L);

driver.findElement(By.xpath("html/body/div[2]/div[2]/div[2]/div/div[3]/div/header/div/button[4]")).click();

    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    a.findValue();
    driver.findElement(By.xpath("//button[contains(.,'submit')]")).click();


    }

}

Class2:

public class FindElement1 { static WebDriver driver = new FirefoxDriver();

public void findValue() {
     driver.get("webaddress");
     By by = By.xpath("//button[contains(.,'submit')]");
     isElementPresent(by);
     driver.findElement(By.xpath(""));

 }
public boolean isElementPresent(By by){
      try{
        driver.findElements(by);
        System.out.println("execute");
       return true;
      }
      catch(NoSuchElementException e){
       return false;
      }
}

}

I want to execute class2's operation into class1 with class1's value in the mentioned place of a.findValue();Is it possible to pass the driver value of class1 into class2

2
As I have understood, you want to keep some common things in a class (such as existence of element, text, WebDriver instance etc.) that can be used from other classes when you need. If I am right to understand your requirement please let me know.Ripon Al Wasim

2 Answers

0
votes

Sure. Just remove the static keyword for the WebDriver from your second class, and create a setter or constructor with the WebDriver.

Instantiate this in your first class and set the WebDriver with your setter or constructor.

0
votes

Create a Common class in which common things can be kept and be used for any class:

package keya;    
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.By;

public class Common {
    protected static WebDriver driver;

    public Common(){
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
    }

    public boolean isElementPresent(By by){
        try{
            driver.findElement(by);
            return true;
        }
        catch(NoSuchElementException e){
            return false;
        }
    }
}

As you are being tested Module3, the code should be as below:

package keya;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;

public class Module3 extends Common{

    public static void main(String...strings) throws Exception{
        Common c = new Common();
        driver.get("webAddress");
        driver.findElement(By.xpath("//*[@id='Username']")).sendKeys("A1");
        driver.findElement(By.xpath("//*[@id='Password']")).sendKeys("1");
        driver.findElement(By.xpath("//*[@id='loginBox']/form/p/button")).click();
        Thread.sleep(2000L);
        driver.findElement(By.xpath("//*[@id='mainNav']/li[2]/a")).click();
        Thread.sleep(2000L);
        driver.findElement(By.xpath("//*[@id='addNewEntryButton']")).click();
        WebElement dropdown = driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[1]/select"));
        List<WebElement> dropOptions = dropdown.findElements(By.tagName("Option"));
        for (int i=0; i<dropOptions.size(); i++)
        {
         System.out.println(dropOptions.get(i).getText());
        }
        Thread.sleep(2000L);
        driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[1]/select")).sendKeys(Keys.ARROW_DOWN);
        driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[5]/input")).sendKeys("5");
        driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[6]/input")).sendKeys("5");
        driver.findElement(By.xpath("//*[@id='saveEntryButton']")).click();
        Thread.sleep(5000L);
        driver.findElement(By.xpath("html/body/div[2]/div[2]/div[2]/div/div[3]/div/header/div/button[4]")).click();
        //Here is the verification whether Submit button is present or not
        c.isElementPresent(By.xpath("//button[contains(.,'submit')]"));
        //Rest of the code is here
    }
}

So You can use isElementPresent() method of Common class for any number of class/module. You can write more class by extending Common class so that you can use some common methods and WebDriver is being instantiated one time only in Common class.