Jump to content

How to return an int in java


newcoder1010

Recommended Posts

    public class a() extends b{}
    //Caller
    Registration1.enterPhoneExt(10);
    }
    
    
    public class b() extends c{
    
         public void enterPhoneExt(int ext) {
            int randomNumber = c.GenerateRandomNumber(ext);
            driver.findElement(PhoneExtField).sendKeys(randomNumber);

     }
    
    }
    
    public class c(int Max){
    
        Random rand = new Random();
        int  n = rand.nextInt(Max) + 1;
        return n;
    }

Hello,  
    
    I am getting error in class b on line:
    

     driver.findElement(PhoneExtField).sendKeys(randomNumber);

     
     It is asking me to change the  data type of randomNumber in class b to charsequence. After I change the data type, I get another error. 
     
     Please advise on how I can return int to caller by keeping the 3 classes?

Link to comment
Share on other sites

So how can I convert to charsequence?

 public void enterPhoneExt(int ext) {

		int randomNumber = SuperClass.GenerateRandomNumber(ext);
		driver.findElement(PhoneExtField).clear();
		char randomNumber1 = (char) randomNumber;
		driver.findElement(PhoneExtField).sendKeys(randomNumber1);
		WaitInSeconds(1000);
}

 

Edited by newcoder1010
Link to comment
Share on other sites

Let's start with example code and then modify it. The following is from the seleniumhq.org example

public class Selenium2Example  {
    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 Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        
        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());
        
        //Close the browser
        driver.quit();
    }
}

 

Link to comment
Share on other sites

 

This code demonstrates the use of methods, similar to what you seemed to be attempting to do. Obviously sendKeys() requires a string rather than an integer.

 

public class Newcoder0001 {
    
    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 myPage
        driver.get("http://www.myPage.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.myPage.com");

      
        enterPhoneExt(driver, 999);
     
/*
        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        
        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());
        
        //Close the browser
        driver.quit();
*/
    }// end of main
    
    static void enterPhoneExt(WebDriver driver, int max){
    
        WebElement element = driver.findElement(By.name("PhoneExtField")); // select element
        element.clear();
        int n = generateRandomNumber(max);
        element.sendKeys( String.valueOf(n) ); // insert int as a string
        
    }// end of method
    
    static int generateRandomNumber(int max){
    
        Random rand = new Random();
        int  n = rand.nextInt(max-1) + 1;
        return n;
        
    }// end of method
    
}// end of class

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...