In this tutorial we will make use of a webservice to translate some text. The text that needs to be translated is passed as a URL parameter. The result can be extracted from the DOM structure within the webbrowser control and is then returned to GuiXT.

We use a TextBox as input and access the text later by using a long text variable. The pushbutton processes an InputScript that calls a VB.NET function which will put the translated text into the second textbox via another long text variable:

pushbutton
(20,1) "Translate" process="translate_text.txt"
text
(22,1) "english"
TextBox
(20,13) (25,100) name="translate_this"
text
(27,1) "german"
TextBox
(26,13) (31,100) name="translated_text"

Inputscript "translate_text.txt":

callvb
tutorials.translator.translate "translate_this" "en" "translated_text" "de"
return

Implementaton of the VB.NET function:

Add a reference to the file guinet.dll to the project and don't forget to compile it as a class library (DLL).

Also add a reference to System.Windows.Forms This library contains the Web Browser Control.



Then move the compiled DLL file and also the file guinet.dll (shipped with GuiXT setup) to the directory provided in the  GuiXT profile.

The VB.NET code looks like this; annotations will follow:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
Imports guinet
Imports System.Windows.Forms

Public Class translator
    Inherits System.Windows.Forms.WebBrowser

    Private done As Boolean


    Public Function translate(ByVal textidIn As String, ByVal LangIn As String,
                              ByVal textidOut As String, ByVal LangOut As String) As String

        'Access to GuiXT variables and SAP GUI Session
        Dim g = New guixt

        'Get content of GuiXT text variable
        Dim translateThis = g.GetText(textidIn)

        ' Example: 
        ' https://www.deepl.com/translator#en/fr/The%20weather%20is%20nice.%0A


        ' Build  translation URL
        Dim url = "https://www.deepl.com/translator#" & LangIn & "/" _
            & LangOut & "/" & System.Uri.EscapeUriString(translateThis)

        done = False
        Navigate(url)

        ' wait until new document is loaded
        While Not done
            Application.DoEvents()
        End While

        ' text translation done
        Dim elems As HtmlElementCollection = Document.GetElementsByTagName("textarea")


        For Each e As HtmlElement In elems

            If e.GetAttribute("dl-test") = "translator-target-input" Then

                For i As Integer = 1 To 10

                    If e.InnerText Is Nothing Then
                        Application.DoEvents()
                        System.Threading.Thread.Sleep(500)
                    Else
                        If e.InnerText.Trim = translateThis.Trim Then
                            Application.DoEvents()
                            System.Threading.Thread.Sleep(500)
                        End If
                    End If

                Next

                g.SetText(textidOut, e.InnerText)
            End If

        Next


        Return ""

    End Function

    ' Document completed
    Public Sub MyDocumentCompleted(ByVal sender As Object,
                                   ByVal e As WebBrowserDocumentCompletedEventArgs) _
        Handles Me.DocumentCompleted

        done = True
    End Sub
End Class

Annotations and hints:

Line 17:
textidln is the name of the long text variable that contains the text to be translated. To read the content we can call guixt.getText.

Line 32:
As we do not know how long the translating will take, we have to wait for the event "documentCompleted" of the browser control.

Line 41:
As soon as the site is completely loaded we can access all of its elements. We are particularly interested in the element containing the translated text.

Line 57:
The translated text is displayed in the SAP GUI window directly by using the command guixt.SetText. "textidOut" here contains the name of the long text variable that was defined by the command TextBox name= within the GuiXT script. This name will be passed as a parameter to the VB.NET function.


Attention for large texts: Due to length restriction of a URL containing the text to be translated only one sentence should be translated at a time.