forked from php-webdriver/php-webdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
Asking User for an Input and Waiting
rooseveltrp edited this page Aug 8, 2013
·
2 revisions
Sooner or later you may need to ask the user for some kind of input. If you use something like the code below:
$session->executeScript("return prompt('Enter your input: ')");
Selenium WebDriver will simply execute the script and continue with rest of the code. So, the strategy is to make PHP or the Selenium Web Driver wait until the user has made an input.
For this example, I will show you how to use JAVA to prompt the user from PHP for a Captcha input and wait until there is an input.
- Open Netbeans and create a new Java Application
- Paste the following codes:
package captcha;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Captcha {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String CaptchaImage = args[0];
try {
BufferedImage image = ImageIO.read(new File(CaptchaImage));
JLabel picLabel = new JLabel(new ImageIcon(image));
JTextField captchaInput = new JTextField();
String SolvedCaptcha = JOptionPane.showInputDialog(null, picLabel);
System.out.println(SolvedCaptcha);
} catch (IOException e){
System.out.println("ERROR: File not found");
}
}
}
- Compile the above application and you should get a file called Captcha.jar
- Copy the Captcha.jar file to the directory where you are executing your PHP script
public function MyPHPFunction(){
$screenshot_directory = "./tmp/myfile.png";
$this->driver->takeScreenshot($screenshot_directory);
...
$captcha_value = system("java -jar Captcha.jar $screenshot_directory");
$this->driver->findElement(WebDriverBy::xpath("//input[@id='captcha']")).sendKeys($captcha_value);
}
Feel free to customize the Captcha.java file to suit your needs.