私はSeleniumをあまり経験していません。私は、以下のことを行い、知識をテストすることを考えました。フォームの名前フィールドに特殊文字がないことを検証します。私はそうすることができませんでした。最初に私は文字を配列に入れて配列から読み込もうとしましたが、私はAlertの失敗メッセージを受け取りました。それから、私は次のように考え、常に出力を "有効"にしました。Selenium Webdriver Javaは名前フィールドを検証します
import junit.framework.Assert;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class NameField {
public static FirefoxDriver fx= new FirefoxDriver();
public static String doCheck()
{
fx.get("http://www.gogamers.com/#!blank/gs4id");
String regex = "^[A-Z0-9+$";
String str=fx.findElement(By.id("comp-iikjotq8nameField")).getText();
fx.findElement(By.id("comp-iikjotq8nameField")).sendKeys("@john");
if (str.matches("[" + regex + "]+")){
System.out.println("Invalid character in Name field");
}
else{
System.out.println("valid");
}
return str;
sendkey(例:John#、@john)を使用して名前を付けた場合、無効なメッセージが表示されます。アサーションを使うべきだと思っていたもう一つのことは?小さなサンプルコードが役立つ最善の方法を提案してください。
私は無効期待していたときに、まだ、有効な私に与えている今日を試してみました新しいコード。誰かが優しく見ることができますか?私は両方の試合を試してみましたが、
パブリッククラスYahooMailを{
public static void main(String[] args) {
FirefoxDriver fx= new FirefoxDriver();
fx.get("https://login.yahoo.com/account/create?");
String title=fx.getTitle();
Assert.assertTrue(title.contains("Yahoo"));
//First I send a text, then I get the text
fx.findElement(By.id("usernamereg-firstName")).sendKeys("$John");
fx.findElement(By.id("usernamereg-firstName")).getText();
//This is the String I want to find
String firstName="John";
//If there are these symbols associated with the name-show invalid
String patternString = ".*$%^#:.*";
Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(firstName);
if(matcher.find()){
System.out.println("Invalid Name");
}
else{
System.out.println("Valid Name");
}
}
}
通常、それだけではなく、匿名のコードの一部の行を掲示するソリューションを説明する方が良いでしょうを使用している必要があります。 [良い答えを書くにはどうすればいいですか]と[コードベースの回答を完全に説明する](https://meta.stackexchange.com)を読むことができます。/questions/114762/explain-entire-%E2%80%8C%E2%80%8Bcode-based-answers) –