Selenium Script to remove users from all groups in Confluence.

Hello Friends,

This is one of the tutorials for Confluence users where they can use the Selenium script as an automation for removing the users from all the groups in Confluence. In this tutorial, I have used one xml file which will contain the usernames and one java file that will contain the selenium script and other java code. The java code will fetch the usernames from the xml file and will perform the required actions at confluence end. The other selenium related details have already been discussed in my previous blogs. So, without discussing it further, lets move to the point.

Following is the xml file named removeUser.xml

In the above file, we have taken 3 users. The java and selenium code for this is in file ConfluenceRemoveUser.java that contains the following:

[java]

package com.code2java.confluence;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import java.io.File;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class ConfluenceRemoveUser extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium(“localhost”, 4444, “*iexplore”, “https://localhost/”);
selenium.start();
}

@Test
public void testConfluenceRemoveUser() throws Exception {

try
{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File(“removeUser.xml”));

doc.getDocumentElement().normalize();
NodeList listOfUsers = doc.getElementsByTagName(“username”);
int totalOptions = listOfUsers.getLength();
System.out.println(“totalOptions is “+totalOptions);

selenium.open(“/confluence/login.action”);
selenium.type(“id=os_username”, “username”);
selenium.type(“id=os_password”, “password”);
selenium.click(“id=loginButton”);
selenium.waitForPageToLoad(“30000”);
System.out.println(“test————–“);
for(int s=0; s<listOfUsers.getLength() ; s++)
{
Node firstOptionNode = listOfUsers.item(s);

try
{
if(firstOptionNode.getNodeType() == Node.ELEMENT_NODE)
{
Element firstUserElement = (Element)firstOptionNode;
NodeList firstNameList = firstUserElement.getElementsByTagName(“list”);
Element firstNameElement = (Element)firstNameList.item(0);
NodeList textFNList = firstNameElement.getChildNodes();

String nameOfOption = ((Node)textFNList.item(0)).getNodeValue().trim();
System.out.println(“The name of user is : “+nameOfOption);

String[] userList = nameOfOption.split(“,”);
for(int i=0;i<userList.length;i++)
{
System.out.println(“The user is “+userList[i]);
selenium.open(“/confluence/admin/users/viewuser.action?username=”+userList[i]);
selenium.waitForPageToLoad(“30000”);
selenium.click(“link=Edit Groups”);
selenium.waitForPageToLoad(“30000”);
selenium.click(“id=editusergroups-selectnone”);
selenium.click(“id=save-btn1”);
selenium.waitForPageToLoad(“30000″);
}
}
}
catch(Exception e)
{
System.out.println(” EXCEPTION :”+ e.getMessage ());
}
finally
{
selenium.click(“css=#logout-link > span”);
selenium.waitForPageToLoad(“30000”);
}
System.out.println(“**************n”);
}

}
catch (SAXParseException err)
{
System.out.println (“** Parsing error” + “, line “+ err.getLineNumber () + “, uri ” + err.getSystemId ());
System.out.println(” ” + err.getMessage ());
}
catch(SAXException ex)
{
Exception x = ex.getException ();
((x == null) ? ex : x).printStackTrace ();
}
catch (Throwable t)
{
t.printStackTrace ();
}
}

@After
public void tearDown() throws Exception {
selenium.stop();
}
}

[/java]

Here, we are reading the users from the xml file and then iterating it via for loop and then performing the selenium script action on them. This is really very much time saving and will help perform your job within time. The same script can be used for adding the users in confluence with little changes.

Hope this will help you.

Regards,

Nikhil Naoghare

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.