Saturday, 8 August 2020

Weblogic Automation via Speech Recognition

 The python code given below mashes modules like speech recognition, Google Text to Speech gtts, selenium web browser automation, pyautogui gui automation to effectively 

1. open weblogic admin console on local desktop

2. supply username,password and click login button

3. Once logged in click the servers link to monitor the configured JVMs

4. Wait for 10 seconds and hit the logout button.

here's the code


#pip install speechrecognition

#pip install pyaudio

import os

import datetime

import speech_recognition as sr

import webbrowser

from time import ctime

#import PyObjC

import random

from gtts import gTTS

import playsound

import subprocess

from selenium import webdriver

import pyautogui

import time

r = sr.Recognizer() # initializing instance of speech recognizer


def record_audio(ask = False):

    with sr.Microphone() as source:     # initializing laptop microphone as source of input

        if ask:

            fiona_speak(ask)              

        audio = r.listen(source)

        voice_data = ''

        try:

            voice_data = r.recognize_google(audio)

        except sr.UnknownValueError:

            fiona_speak('Can you please repeat your last command')

        except sr.RequestError:

            fiona_speak('Speech Server is down')

        return voice_data

# function to take input as text strings, create mp3 files and simulate computer speaking to you

def fiona_speak(audio_string):

    tts = gTTS(text=audio_string, lang='en')

    r = random.randint(1,10000000)

    audio_file = 'webauto-' + str(r) + '.mp3'

    tts.save(audio_file)

    playsound.playsound(audio_file)

    print(audio_string)

    os.remove(audio_file)


# actual function which has tasks associated with specific voice inputs

def respond(voice_data):

    if 'what is your name' in voice_data:

        fiona_speak('my name is fiona')

    if 'what time is it' in voice_data:

        fiona_speak(ctime())

    if 'who is the richest person' in voice_data:

        fiona_speak('The one who is happiest')

    if 'find something' in voice_data:

        search = record_audio('what do you want to search for')

        url = 'https://google.com/search?q=' + search

        webbrowser.register('chrome',None,

    webbrowser.BackgroundBrowser("C://Program Files x86)//Google//Chrome//Application//chrome.exe"))

        webbrowser.get('chrome').open_new(url)

        fiona_speak('here is what if found for ' + search)

    if 'open application' in voice_data:

        #automatically opens weblogic console page enters your id/password and clicks on next button

        fiona_speak('checking domain')

        driver = webdriver.Chrome("chromedriver.exe") #initializing chrome browser

        driver.get("http://dtawade-in2:7002/console")

        driver.maximize_window()

        driver.find_element_by_name("j_username").send_keys("admin") # entering username

        driver.find_element_by_name("j_password").send_keys("admin123") # entering password

        pyautogui.FAILSAFE = False #disabling pyautogui failsafe

        pyautogui.click(1739, 728) # clicking mouse button, the x,y coordinates need to be customized*

        driver.find_element_by_xpath("//*[@id='HomePagePortlet']/div/div[6]/div[3]/ul/li[1]/a").click()# clicking servers button on home page

        driver.implicitly_wait(25)

        time.sleep(10) # user induced wait of 10sec

        driver.find_element_by_xpath("//*[@id='topMenu']/ul/li[2]/a").click() # click logout on home page   

    if 'exit' in voice_data:

        exit()

    return respond


time.sleep(1)

fiona_speak('How can I help you')

while 1:

    voice_data = record_audio()

    respond(voice_data)

# above while to keep looping the program


* python mouse coordinate finder program. To know the x and y coordinates of any button or link that you want to click.

1. Position mouse on the link/button

2. Without touching the mouse shift to python or visual studio code window and execute the followin line

3. Substitute the values, returned by the code below, in above code.


import pyautogui

print(pyautogui.position())


Automation may be a good thing, but don't forget that it began with Frankenstein.” – Anonymous 💾💻