2013-06-11 3 views
0

셀레늄을 사용하여 cleditor 텍스트 필드의 본문을 C#으로 설정할 수 있지만이 프로세스는 매우 어려워 보입니다. 누구든지 내가 4 살처럼 설명 할 수 있을까?셀렌과 cleditor에 C# 사용하기

+0

, 당신은 텍스트를 입력 의미합니까? – neo

+0

예, 전자 메일 생성자로 사용되며 입력 한 텍스트는 다음 주소로 이메일로 전송됩니다 – user2475311

+0

어떤 Selenium을 사용합니까? 웹 드라이버 또는 RC? – neo

답변

0

Google 크롬에서 페이지로 이동하면 텍스트 입력란의 편집 가능 영역을 마우스 오른쪽 버튼으로 클릭합니다. 요소 검사를 클릭합니다. html이 열리면 강조 표시된 요소를 마우스 오른쪽 단추로 클릭하고 XPath 복사를 클릭하십시오. 셀레늄 웹 드라이버 기본 셀레늄 논리는 다음 첫번째 요소를 찾으로 작업을 수행 할 것입니다

IWebElement textField =  driver.FindElement(By.XPath("Paste what you got from CHROME")); 
textField.SendKeys("Desired Text"); 
1

에서

.

그러나이 경우, 다음과 같은 어려움이 있습니다

  1. 에디터가 iframe을 편집기는 ID, 이름 또는 의미있는 SRC
  2. 이없는 iframe
  3. 에 입력 또는 텍스트 영역이 아니라 body 그 자체

은 iframe을로 이동하는 방법 iframe을 찾을

사용 파이어 폭스 + 방화범 + Firepath을 (의 Arran의 링크를 볼 수있는 좋은 튜토리얼이어야 함). 당신이 볼 수 있듯이

Firefox+Firebug+Firepath usage

이 페이지에서 네 iframe을 거기, 당신은 편집기 프레임이 아닌 다른 프레임으로 전환하려면 다음 방법 중 하나가 필요합니다. (source)

IWebDriver Frame(int frameIndex); // works but not desirable, as you have 4 frames, index might be changing 
IWebDriver Frame(string frameName); // not working, your editor doesn't have frameName or id. 
IWebDriver Frame(IWebElement frameElement); // the way to go, find frame by xpath or css selector in your case 

그래서 우리는이 :

IWebElement iframe = driver.FindElement(By.XPath("//iframe[@src='javascript:true;']")); 
driver.SwitchTo().Frame(iframe); 

드라이버가 iframe을 내부되면

이, 방화범을 통해, 당신이 볼 수있는 편집기에 키를 전송하는 방법 편집기는 실제로 body이며 input 또는 textarea이 아닙니다.

그래서 body 요소를 찾아서 지우고 키를 보내야합니다. Clear()은 body 요소에서 작동하지 않을 수 있으므로 IJavaScriptExecutor을 사용하거나 Control+a을 보내어 모두 먼저 선택해야합니다. 텍스트 후

를 iframe 외부

스위치가 에디터로 보냈습니다, 당신은 나가 driver.SwitchTo().DefaultContent();를 사용할 수 있습니다.

몸을 설정하여 완성 된 코드

using Microsoft.VisualStudio.TestTools.UnitTesting; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Firefox; 

namespace SOTest { 
    [TestClass] 
    public class TestCLEditor { 
     [TestMethod] 
     public void TestMethod1() { 
      IWebDriver driver = new FirefoxDriver(); 
      driver.Navigate().GoToUrl("http://premiumsoftware.net/CLEditor"); 

      // find frames by src like 'javascript:true;' is really not a good idea, but works in this case 
      IWebElement iframe = driver.FindElement(By.XPath("//iframe[@src='javascript:true;']")); 
      driver.SwitchTo().Frame(iframe); 

      IWebElement body = driver.FindElement(By.TagName("body")); // then you find the body 
      body.SendKeys(Keys.Control + "a"); // send 'ctrl+a' to select all 
      body.SendKeys("Some text"); 

      // alternative way to send keys to body 
      // IJavaScriptExecutor jsExecutor = driver as IJavaScriptExecutor; 
      // jsExecutor.ExecuteScript("var body = document.getElementsByTagName('body')[0]; body.innerHTML = 'Some text';"); 

      driver.Quit(); 
     } 
    } 
}