Selenium and wordpress: new post-test

I looked at Selenium a bit and I like it, since I know some Java programs and I think Java and C # are pretty straightforward for simple things like this.

However, I am struggling with a test that creates a new post in Wordpress from the Dashboard page:

This is the Selena code (in C #): (The driver instance is obviously the driver class that I created - to launch the browser and connect to the wordpress site.)

1: Driver.Instance.FindElement(By.Id("title)).SendKeys(title);
2: Thread.Sleep(1000);
3: 
4: Instance.SwitchTo().Frame("content_ifr");
5: Thread.Sleep(1000);
6: 
7: Driver.Instance.SwitchTo().ActiveElement().SendKeys("something");

Now what happens is that the title is easy to find (by id, so I did not expect problems there), and I can easily insert the title text (line 1).

But the inline frame for the message body causes problems. When you start the test after filling in the topic, the cursor changes the area of ​​the body (line 4) - as planned. However, nothing else is happening. The SendKeys ("string") (ine 7) method does not seem to work there.

Any ideas?

EDIT: Of course, the important information is that the iframe in Wordpress just loads the TinyMCE editor. Thus, in the page source there is only a body tag with loading the javascript editor.

EDIT2: Of course, something suddenly changed. Without ANY change on the wordpress page, "content_ifr" is now suddenly absent (? !!!!!?). The Selenium test fails with "inability to find a frame ...", and it is also unexpectedly missing from the page source.

enter image description here

EDIT3: I also noticed something:

Driver.Instance.SwitchTo().Frame(iframe);
Driver.Instance.FindElement(By.Id("tinymce")).SendKeys("message body");

, mce, .SwitchTo(). - . - . - Selenium. Selenium , .

, SendKeys(), . , , , , . .

EDIT4 (): , , IJavaScriptExecutor, .

+1
4

#, . , , , .

Selenium, : , , . , 100 , 1 , 1 . . .

. , "" . , , , . , , : , .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Support.Events;

namespace SeleniumTest
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new OpenQA.Selenium.Firefox.FirefoxDriver();
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));

            // enter your configurations here 
            driver.Navigate().GoToUrl("http://localhost/wordpress/wp-admin/post-new.php");
            driver.FindElement(By.Id("user_login")).SendKeys("admin");
            driver.FindElement(By.Id("user_pass")).SendKeys("yourpassword");

            driver.FindElement(By.Id("wp-submit")).Click();

            driver.FindElement(By.Id("title")).SendKeys("the title");
            var iframe = driver.FindElement(By.Id("content_ifr"));
            driver.SwitchTo().Frame(iframe);


            // your solution which works in my instance
            //driver.SwitchTo().ActiveElement().SendKeys("hello tiny mce from selenium");

            // send keys with exact element
            //driver.FindElement(By.Id("tinymce")).SendKeys("hello tiny mce from selenium");

            // javascript - 1
            IJavaScriptExecutor js = driver as IJavaScriptExecutor;
            var tinymce = driver.FindElement(By.Id("tinymce"));
            IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
            executor.ExecuteScript("arguments[0].innerHTML = 'hello tiny mce via javascript'", tinymce);
            // javascript - 0



            driver.SwitchTo().DefaultContent();

            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(11));
            wait.Until((d) => { return !d.FindElement(By.Id("publish")).GetAttribute("class").Contains("disabled"); }); // wait for publish button to be enabled
            driver.FindElement(By.Id("publish")).Click();
            driver.FindElement(By.Id("message")); // wait for message on next page to verify it is posted
            driver.Close();
        }
    }
}
+2

Java- TinyMCE :

public void entersTopicOfBody(String textToBeTyped, WebDriver driver) {
        driver.switchTo().frame("content_ifr");
        WebElement body = driver.findElement(By.xpath("//body"));
        body.click();
        JavascriptExecutor executor = (JavascriptExecutor)driver;
        executor.executeScript("arguments[0].innerHTML = '"+ textToBeTyped+"'", body);
        driver.switchTo().defaultContent();
    }
+3

PHP- olyv:

$content = 'my text';
$this->frame( 'content_ifr' );
$body = $this->byXPath( '//body' );
$body->click();
$script = 'arguments[0].innerHTML = "" + arguments[1] + ""; ';
$this->execute( [
            'script' => $script,
            'args'   => [ $body->toWebDriverObject(), $content ],
        ]
    );
$this->frame( null );
0

, , , ( ) , , . , - .

There is no need to switch frames. What you want to do is "click" on the button in the upper right corner of the text editor that says "Text", which has id = "content-html". Now you can "send keys" to the text field that has id = "content".

Here is the Python code that does just that:

driver.find_element_by_id("content-html").click()
driver.find_element_by_id("content").send_keys("Some text...")

Hope help

0
source

Source: https://habr.com/ru/post/1527119/


All Articles