Selenium and wordpress: New post test

0
0

I’ve been looking a bit at Selenium, and I’m beginning to like it, since I know some Java programming and find both Java and C# pretty straight-forward for simple things like this.

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

This is the Selenium code (in C#):
(The Driver instance is, obviously, a driver class I’ve created – for starting the browser and connecting 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 easily found (by ID, so I wouldn’t expect problems there), and I can easily insert the title text (line 1).

But the inline frame for the post body is causing problems. When running the test, after the topic is filled in, the cursor changes to the body area (line 4) – as planned. However, nothing more happens. The SendKeys(“string”) method (ine 7) doesn’t seem to work there.

Any ideas?

EDIT: Of course – an important piece of information is that the iframe in Wordpress simply loads a TinyMCE editor. So, in the page source, there’s only a body tag with the javascript loading of the editor.

EDIT2: Of course, something suddenly changed. Without ANY change to the wordpress page, the “content_ifr” is now suddenly missing (?!!!!!?) The Selenium test fails with “unable to locate frame…”, and it’s also suddenly 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");

It’s the SECOND line that makes the cursor switch to the mce field, not the line with the .SwitchTo(). However, I need the first line – the second line does nothing on its own. This is approaching something really stupid. I’ve been looking for a solution to this for a week – this doesn’t exactly bode well for Selenium. The Selenium user group doesn’t even want to answer when I ask them.

Also – if I skip the SendKeys() method in the second line, nothing happens. So, it seems that the two lines does ALLMOST what it should, right up to and including placing the cursor in the correct spot. But it never sends any text.

EDIT4 (last): After actually figuring out how to use IJavaScriptExecutor, it works using the solution(s) below.

  • You must to post comments
0
0

Below is some C# code that publishes a post. I think the main issues you have are due to timing issues.

I’ve done a bit of Selenium recently and I favour implicit waits: it waits for a maximum time period for the item to be available, but returns as soon as possible. So you can specify a max wait of 100 seconds, but if it finds it in 1 second, it will only wait 1 second. Much more efficient vs sleeping for an arbitrary length of time. See this post about Implicit and Explicit waits

But even with implicit waits, it may not solve all issues. When coding the sample below, I ran into the issue where the “Publish” button was disabled and re-enabled after some time. And that’s when you have to look at the code to see what it is doing as well. It’s times such as these where sleeps can help you fix the problem for a quick fix if you do not wish to debug too much: just be sure to set a large enough sleep time and be wary that it could be inconsistent in the future.

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();
}
}
}
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website