newcoder1010 Posted August 8, 2017 Share Posted August 8, 2017 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 More sharing options...
justsomeguy Posted August 8, 2017 Share Posted August 8, 2017 If sendKeys expects a charsequence, why are you passing an int to it? Shouldn't you do something with that random number, like converting it to a character, before passing it to sendKeys? Link to comment Share on other sites More sharing options...
newcoder1010 Posted August 9, 2017 Author Share Posted August 9, 2017 (edited) 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 August 9, 2017 by newcoder1010 Link to comment Share on other sites More sharing options...
davej Posted August 9, 2017 Share Posted August 9, 2017 This code makes no sense. Is this supposed to be Selenium/WebDriver stuff? Link to comment Share on other sites More sharing options...
newcoder1010 Posted August 9, 2017 Author Share Posted August 9, 2017 yes Link to comment Share on other sites More sharing options...
davej Posted August 9, 2017 Share Posted August 9, 2017 What are you actually trying to do? Link to comment Share on other sites More sharing options...
newcoder1010 Posted August 9, 2017 Author Share Posted August 9, 2017 my code has details. Simply trying to generate a random number and then enter it as phone extension. Link to comment Share on other sites More sharing options...
davej Posted August 9, 2017 Share Posted August 9, 2017 Do you really need to use multiple classes and inheritance? Link to comment Share on other sites More sharing options...
newcoder1010 Posted August 9, 2017 Author Share Posted August 9, 2017 Any solution will help. Link to comment Share on other sites More sharing options...
davej Posted August 9, 2017 Share Posted August 9, 2017 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 More sharing options...
newcoder1010 Posted August 9, 2017 Author Share Posted August 9, 2017 I asked how to generate a random number and then enter it. Your example is not relevant. Link to comment Share on other sites More sharing options...
davej Posted August 9, 2017 Share Posted August 9, 2017 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now