2016-05-01 2 views
1

私は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"); 
    } 
} 

}

答えて

1

あなたはどの英数字以外の文字にマッチする正規表現を修正し、代わりにPatternMatcherを使用できます:

Pattern p = Pattern.compile("\\W"); 
Matcher m = p.matcher(str); 
if (m.find()) { 
    System.out.println("Invalid character in Name field"); 
} 
else { 
    System.out.println("valid"); 
} 
0

現在作業中ですが、問題はキャプチャしていないことですsendKeysの値私はのgetAttribute

f.get("https://mail.yahoo.com"); 
     f.findElement(By.id("login-username")).sendKeys("jj%jo.com"); 


     //The getAttribute method returns the value of an attribute of an HTML Tag; 
     //for example if I have an input like this: 
     WebElement element = f.findElement(By.id("login-username")); 
     String text = element.getAttribute("value"); 
     System.out.println(text); 

if((text).contains("@")){ 
    System.out.println("pass"); 
} 
else{ 
    System.out.println("not pass"); 
} 


    enter code here 
0
public class Personal_loan { 

public String verified_number(String inputNumber) // pass the parameter 
{ 
    String validation; 

    String regexNum = "[0-9]+";   //"[A-Za-z]";//"^[A-Z+$]"; 

    if (inputNumber.matches("[" + regexNum + "]+")) 
    { 
     System.out.println("valid"); 
     validation="valid"; 
    } 
    else{ 

     System.out.println("Invalid character in Name field"); 
     validation="invalid"; 
    } 
    return validation; 

} 

public String verified_str(String inputStr) 
{ 
    String regexString = "[A-Za-z]";//"^[A-Z+$]"; 

    if (inputStr.matches("[" + regexString + "]+")) 
    { 
     System.out.println("valid"); 
    } 
    else{ 

     System.out.println("Invalid character in Name field"); 
    } 
    return null; 

} 



public static void main(String[] args) { 


    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe"); 
    WebDriver driver = new ChromeDriver(); 
    driver.get("https://www.iservefinancial.com/"); 
    driver.findElement(By.xpath("(//DIV[@itemprop='name'])[1]")).click(); 
    WebElement LoanAmount =driver.findElement(By.xpath("//INPUT[@id='amount_qa']")); 
    WebElement Income =driver.findElement(By.xpath("//INPUT[@id='income_qa']")); 
    LoanAmount.sendKeys("12345"); 
    Income.sendKeys("amount"); 

    Personal_loan pl=new Personal_loan(); //creating object 


    String g = LoanAmount.getAttribute("value"); // store the value in string 
    String incomevalue = Income.getAttribute("value"); 


    String lavalid=pl.verified_number(g); 
    String income_valid = pl.verified_number(incomevalue); 


    System.out.println("Loan Amount "+lavalid); 
    System.out.println("income Amount "+income_valid); 





} 

}

+0

通常、それだけではなく、匿名のコードの一部の行を掲示するソリューションを説明する方が良いでしょうを使用している必要があります。 [良い答えを書くにはどうすればいいですか]と[コードベースの回答を完全に説明する](https://meta.stackexchange.com)を読むことができます。/questions/114762/explain-entire-%E2%80%8C%E2%80%8Bcode-based-answers) –